【Java】——Json反序列化为Java对象

来源:互联网 发布:淘宝上最好卖的东西 编辑:程序博客网 时间:2024/06/09 15:27

【项目需求】

   最近做项目的时候,功能是将一个表单和一个datagrid中的集合中的数据一起传到后台去,也就是将接送。


【思路】

  1、在之前做过的功能中,我们用过@requestMap来接收传过来的list对象集合,所以我也想用这种方法去接收。于是将我的jsonStr拼接到url中。将form表单对象值放到data中。尝试之后,发现无法接收,因为传递过来的是一个字符串。

 2、用字符串接收,然后在后台去解析。

   我知道可以用split函数去解析它,但是不想用它,想用一个简单的方法,它可以直接转换为后台的list对象集合。然后再循环遍历保存到数据库中。

 

【关键代码】


  1. List<Point> points = new ArrayList<Point>();  
  2. ObjectMapper om = new ObjectMapper();  
  3.        
  4. JsonNode node=om.readTree(jsonStr);  
  5. points= (List<Point>) om.readValue(node.toString(), new TypeReference<List<Point>>() {});  

【总结】

 有些问题,只要我们想要去解决,就一定能够解决掉的。下面是上网查到的一些资料,希望对大家有所帮助!

 1、实体对象 

Java代码  
  1. package com.boonya.gps;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import org.codehaus.jackson.annotate.JsonProperty;  
  6.   
  7. public class Point implements Serializable{  
  8.       
  9.     private static final long serialVersionUID = -8359918523259373062L;  
  10.     private double lat;  
  11.     private double lng;  
  12.       
  13.     public double getLat() {  
  14.         return lat;  
  15.     }  
  16.       
  17.     public Point(@JsonProperty("lat"double lat,@JsonProperty("lng"double lng) {  
  18.         this.lat = lat;  
  19.         this.lng = lng;  
  20.     }  
  21.   
  22.     public void setLat(double lat) {  
  23.         this.lat = lat;  
  24.     }  
  25.       
  26.     public double getLng() {  
  27.         return lng;  
  28.     }  
  29.       
  30.     public void setLng(double lng) {  
  31.         this.lng = lng;  
  32.     }  
  33.   
  34.     @Override  
  35.     public String toString() {  
  36.         return "Point(lat,lng)="+" {lat:"+lat+","+"lng:"+lng+"}";  
  37.     }  
  38.   
  39. }  

2、实现JSON数据反序列化为Java对象 

Java代码  
  1. package com.boonya.gps;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.codehaus.jackson.JsonGenerationException;  
  7. import org.codehaus.jackson.JsonNode;  
  8. import org.codehaus.jackson.JsonParseException;  
  9. import org.codehaus.jackson.JsonProcessingException;  
  10. import org.codehaus.jackson.map.JsonMappingException;  
  11. import org.codehaus.jackson.map.ObjectMapper;  
  12. import org.codehaus.jackson.type.TypeReference;  
  13. /** 
  14.  * JSON数据反序列化为Java对象或对象集合 
  15.  * @author BOONYACHENGDU@GMAIL.COM 
  16.  * @date  2013-8-28 
  17.  */  
  18. public class JSONSeriaToObject {  
  19.       
  20.     /** 
  21.      * 对象转JSON 
  22.      * @param obj 
  23.      * @return 
  24.      */  
  25.     public  String getJsonFromObject(Object obj){  
  26.         ObjectMapper om = new ObjectMapper();  
  27.         try {  
  28.             return om.writeValueAsString(obj);  
  29.         } catch (JsonGenerationException e) {  
  30.             e.printStackTrace();  
  31.         } catch (JsonMappingException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return "";  
  37.     }  
  38.       
  39.     /** 
  40.      * JSON发序列化为Java对象 
  41.      * @param jsonStr 
  42.      * @return 
  43.      */  
  44.     public  Point  getPointByJsonString(String jsonStr){  
  45.         Point point =null;  
  46.         ObjectMapper om = new ObjectMapper();  
  47.         try {  
  48.             JsonNode node=om.readTree(jsonStr);  
  49.             point= (Point) om.readValue(node.toString(),Point.class);  
  50.         } catch (JsonParseException e) {  
  51.             e.printStackTrace();  
  52.         } catch (JsonMappingException e) {  
  53.             e.printStackTrace();  
  54.         } catch (JsonGenerationException e) {  
  55.             e.printStackTrace();  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.         System.out.println("<:success single point:>"+point.toString());  
  60.         return point;  
  61.     }  
  62.       
  63.     /** 
  64.      * 拼接Json数据的字符串转化为标准Json格式字符串 
  65.      * @param str 
  66.      * @return 
  67.      */  
  68.     public  String  getJsonNodeStringByString(String str){  
  69.         ObjectMapper om = new ObjectMapper();  
  70.         try {  
  71.             JsonNode node=om.readTree(str);  
  72.             return node.toString();  
  73.         } catch (JsonProcessingException e) {  
  74.             e.printStackTrace();  
  75.         } catch (IOException e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.         return "";  
  79.     }  
  80.       
  81.     /** 
  82.      * JSON发序列化为Java对象集合 
  83.      * @param jsonStr 
  84.      * @return 
  85.      */  
  86.     @SuppressWarnings("unchecked")  
  87.     public  List<Point>  getPointsByJsonString(String jsonStr){  
  88.         List<Point> points = new ArrayList<Point>();  
  89.         ObjectMapper om = new ObjectMapper();  
  90.         try {  
  91.             JsonNode node=om.readTree(jsonStr);  
  92.             points= (List<Point>) om.readValue(node.toString(), new TypeReference<List<Point>>() {});  
  93.         } catch (JsonParseException e) {  
  94.             e.printStackTrace();  
  95.         } catch (JsonMappingException e) {  
  96.             e.printStackTrace();  
  97.         } catch (JsonGenerationException e) {  
  98.             e.printStackTrace();  
  99.         } catch (IOException e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.         for (int i = 0; i < points.size(); i++) {  
  103.             System.out.println("<:success index "+i+":>"+points.get(i).toString());  
  104.         }  
  105.         return points;  
  106.     }  
  107.   
  108.     /** 
  109.      * JSON数据反序列化为Java对象的测试入口 
  110.      * @param args 
  111.      * @throws JsonGenerationException 
  112.      * @throws JsonMappingException 
  113.      * @throws IOException 
  114.      */  
  115.     public static void main(String[] args) throws JsonGenerationException,JsonMappingException, IOException {  
  116.         JSONSeriaToObject jsto=new JSONSeriaToObject();  
  117.           
  118.         //原始数据格式  
  119.         System.out.println("----------------------------------Jackson JSON(list<T>)  to Java  Object-----------------------------");   
  120.         List<Point> points = new ArrayList<Point>();  
  121.         points.add(new Point(34.2332,104.46664));  
  122.         String json=jsto.getJsonFromObject(points);  
  123.         System.out.println(json);  
  124.         //JSON序列化Java对象  
  125.         jsto.getPointsByJsonString(json);  
  126.           
  127.         System.out.println("----------------------------------Jackson JSON(T)  to Java  Object-----------------------------");  
  128.         Point point=new Point(34.5332,104.76664);  
  129.         String json2=jsto.getJsonFromObject(point);  
  130.         System.out.println(json2);  
  131.         //JSON序列化Java对象  
  132.         jsto.getPointByJsonString(json2);  
  133.           
  134.         //JSON序列化为Java对象  
  135.         System.out.println("----------------------------------Ping string JSON  to Java  Object-----------------------------");  
  136.         String latlngs =new String("[{\"lat\":34.232013,\"lng\":103.466002},{\"lat\":34.531939,\"lng\":103.665816}]");  
  137.         //经测试以下数据若调用getJsonFromObject是不行的会产生异常  
  138.         String json3=jsto.getJsonNodeStringByString(latlngs);  
  139.         System.out.println(json3);  
  140.           
  141.         //JSON序列化Java对象  
  142.         jsto.getPointsByJsonString(json3);  
  143.   
  144.     }  
  145.   
  146. }  

3、打印结果 

Java代码  
  1. ----------------------------------Jackson JSON(list<T>)  to Java  Object-----------------------------  
  2. [{"lat":34.2332,"lng":104.46664}]  
  3. <:success index 0:>Point(lat,lng)= {lat:34.2332,lng:104.46664}  
  4. ----------------------------------Jackson JSON(T)  to Java  Object-----------------------------  
  5. {"lat":34.5332,"lng":104.76664}  
  6. <:success single point:>Point(lat,lng)= {lat:34.5332,lng:104.76664}  
  7. ----------------------------------Ping string JSON  to Java  Object-----------------------------  
  8. [{"lat":34.232013,"lng":103.466002},{"lat":34.531939,"lng":103.665816}]  
  9. <:success index 0:>Point(lat,lng)= {lat:34.232013,lng:103.466002}  
  10. <:success index 1:>Point(lat,lng)= {lat:34.531939,lng:103.665816

0 0