java中json进阶_Java 进阶 & JackJson 的使用

本文详细介绍了如何使用Jackson库在Java中进行JSON转换,包括对象转JSON字符串、List转JSON字符串、JSON字符串转对象以及JSON字符串转集合。示例代码展示了ObjectMapper的使用方法,如writeValueAsString()、readValue()等方法。
摘要由CSDN通过智能技术生成

需要包:

jackson-core-2.2.3.jar(核心jar包)

jackson-annotations-2.2.3.jar(该包提供Json注解支持)

jackson-databind-2.2.3.jar

com.fasterxml.jackson.core

jackson-core

2.9.1

com.fasterxml.jackson.core

jackson-annotations

2.9.1

com.fasterxml.jackson.core

jackson-databind

2.9.1

1、指定对象Class转成 json字符串

User user = new User();

user.setName("小民");

user.setEmail("xiaomin@sina.com");

user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

user.setBirthday(dateformat.parse("1996-10-01"));

/**

ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。

ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。

writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。

writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。

writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。

writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。

*/

ObjectMapper mapper = new ObjectMapper();

//User类转JSON

{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}

String json = mapper.writeValueAsString(user);

System.out.println(json);

输出:

{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}

2、List集合转化成json字符串

List users = new ArrayList();

users.add(user);

String jsonlist = mapper.writeValueAsString(users);

System.out.println(jsonlist);

输出:

[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]

3、Json字符串转化成指定Class类

String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";

/**

* ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。

*/

ObjectMapper mapper = new ObjectMapper();

User user = mapper.readValue(json, User.class);

System.out.println(user);

输出:

User{name='小民aa', age=25, birthday=Tue Oct 01 00:00:00 CST 1996, email='xiaomin@sina.com'}

4、Json字符串转化成集合List

方法一:

String jsonString="[{'id':'1'},{'id':'2'}]";

ObjectMapper mapper = new ObjectMapper();

JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);

//如果是Map类型 mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);

List lst = (List)mapper.readValue(jsonString, javaType);

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";

ObjectMapper mapper = new ObjectMapper();

List beanList = mapper.readValue(jsonString, new TypeReference>() {});

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}

Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值