【Java】——Json反序列化为Java对象
2016-07-24 21:15
453 查看
【项目需求】
最近做项目的时候,功能是将一个表单和一个datagrid中的集合中的数据一起传到后台去,也就是将接送。【思路】
1、在之前做过的功能中,我们用过@requestMap来接收传过来的list对象集合,所以我也想用这种方法去接收。于是将我的jsonStr拼接到url中。将form表单对象值放到data中。尝试之后,发现无法接收,因为传递过来的是一个字符串。2、用字符串接收,然后在后台去解析。
我知道可以用split函数去解析它,但是不想用它,想用一个简单的方法,它可以直接转换为后台的list对象集合。然后再循环遍历保存到数据库中。
【关键代码】
List<Point> points = new ArrayList<Point>();ObjectMapper om = new ObjectMapper();
JsonNode node=om.readTree(jsonStr);
points= (List<Point>) om.readValue(node.toString(), new TypeReference<List<Point>>() {});
【总结】
有些问题,只要我们想要去解决,就一定能够解决掉的。下面是上网查到的一些资料,希望对大家有所帮助!1、实体对象
Java代码package com.boonya.gps;
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonProperty;
public class Point implements Serializable{
private static final long serialVersionUID = -8359918523259373062L;
private double lat;
private double lng;
public double getLat() {
return lat;
}
public Point(@JsonProperty("lat") double lat,@JsonProperty("lng") double lng) {
this.lat = lat;
this.lng = lng;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
@Override
public String toString() {
return "Point(lat,lng)="+" {lat:"+lat+","+"lng:"+lng+"}";
}
}
2、实现JSON数据反序列化为Java对象
Java代码package com.boonya.gps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
/**
* JSON数据反序列化为Java对象或对象集合
* @author BOONYACHENGDU@GMAIL.COM
* @date 2013-8-28
*/
public class JSONSeriaToObject {
/**
* 对象转JSON
* @param obj
* @return
*/
public String getJsonFromObject(Object obj){
ObjectMapper om = new ObjectMapper();
try {
return om.writeValueAsString(obj);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* JSON发序列化为Java对象
* @param jsonStr
* @return
*/
public Point getPointByJsonString(String jsonStr){
Point point =null;
ObjectMapper om = new ObjectMapper();
try {
JsonNode node=om.readTree(jsonStr);
point= (Point) om.readValue(node.toString(),Point.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("<:success single point:>"+point.toString());
return point;
}
/**
* 拼接Json数据的字符串转化为标准Json格式字符串
* @param str
* @return
*/
public String getJsonNodeStringByString(String str){
ObjectMapper om = new ObjectMapper();
try {
JsonNode node=om.readTree(str);
return node.toString();
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* JSON发序列化为Java对象集合
* @param jsonStr
* @return
*/
@SuppressWarnings("unchecked")
public List<Point> getPointsByJsonString(String jsonStr){
List<Point> points = new ArrayList<Point>();
ObjectMapper om = new ObjectMapper();
try {
JsonNode node=om.readTree(jsonStr);
points= (List<Point>) om.readValue(node.toString(), new TypeReference<List<Point>>() {});
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < points.size(); i++) {
System.out.println("<:success index "+i+":>"+points.get(i).toString());
}
return points;
}
/**
* JSON数据反序列化为Java对象的测试入口
* @param args
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static void main(String[] args) throws JsonGenerationException,JsonMappingException, IOException {
JSONSeriaToObject jsto=new JSONSeriaToObject();
//原始数据格式
System.out.println("----------------------------------Jackson JSON(list<T>) to Java Object-----------------------------");
List<Point> points = new ArrayList<Point>();
points.add(new Point(34.2332,104.46664));
String json=jsto.getJsonFromObject(points);
System.out.println(json);
//JSON序列化Java对象
jsto.getPointsByJsonString(json);
System.out.println("----------------------------------Jackson JSON(T) to Java Object-----------------------------");
Point point=new Point(34.5332,104.76664);
String json2=jsto.getJsonFromObject(point);
System.out.println(json2);
//JSON序列化Java对象
jsto.getPointByJsonString(json2);
//JSON序列化为Java对象
System.out.println("----------------------------------Ping string JSON to Java Object-----------------------------");
String latlngs =new String("[{\"lat\":34.232013,\"lng\":103.466002},{\"lat\":34.531939,\"lng\":103.665816}]");
//经测试以下数据若调用getJsonFromObject是不行的会产生异常
String json3=jsto.getJsonNodeStringByString(latlngs);
System.out.println(json3);
//JSON序列化Java对象
jsto.getPointsByJsonString(json3);
}
}
3、打印结果
Java代码----------------------------------Jackson JSON(list<T>) to Java Object-----------------------------
[{"lat":34.2332,"lng":104.46664}]
<:success index 0:>Point(lat,lng)= {lat:34.2332,lng:104.46664}
----------------------------------Jackson JSON(T) to Java Object-----------------------------
{"lat":34.5332,"lng":104.76664}
<:success single point:>Point(lat,lng)= {lat:34.5332,lng:104.76664}
----------------------------------Ping string JSON to Java Object-----------------------------
[{"lat":34.232013,"lng":103.466002},{"lat":34.531939,"lng":103.665816}]
<:success index 0:>Point(lat,lng)= {lat:34.232013,lng:103.466002}
<:success index 1:>Point(lat,lng)= {lat:34.531939,lng:103.665816}
相关文章推荐
- Cannot forward after response has been committed
- Java自学视频整理(持续更新中...)
- Java 基础之(十一)一维数组
- HashMap的实现原理
- Java源码-简单手绘程序
- JAVA实现观察者模式
- Struts2数据传输的背后机制
- java多线程--condition条件
- Java 读取配置文件中的信息 中文乱码
- JavaServlet实现文件上传
- Java虚拟机知识整理——类加载的过程
- java学习之路 之 基本语法-程序流程控制-循环结构-嵌套循环练习题
- 深入理解Spring4框架(二)——容器
- java学习之路 之 基本语法-程序流程控制-循环结构-for 循环练习题
- myeclipse关联weblogic时提示 D:\program\weblogic is not a valid BEA WebLogic Server v10.x installation directory
- SpringMVC处理请求流程(转载)
- 【java基础】JAVA常用正则表达式
- JAVA实现单例模式
- Java 不用数组打印杨辉三角
- 坦克大战_我方坦克发射子弹