如何使用jackson反序列化json到对象

如何使用jackson反序列化json到对象,第1张

import javautilList; import orgcodehausjacksonJsonNode; import orgcodehausjacksonmapObjectMapper; import orgcodehausjacksontypeTypeReference; public class TestJackson { private static String json = "{\"indexs\":[{\

一、Java对象 ⇒ JSON

11 转换方式

首先创建转换对象ObjectMappera

ObjectMapper mapper = new ObjectMapper();

该对象主要有两个转换方法

第二种方式:

mapperwriteValue(参数1,p1);

关于参数1

File:将obj对象转换为json字符串,并保存到指定的文件中

Write:将obj对象转换为json字符串,并将json填充到字符输出流中

OutputStream:将obj对象转换为json字符串,并将json填充到字节输出流中

比如:

将JSON字符串数据写入到testtxt文件中:

mapperwriteValue(new File("/Users//Desktop/testtxt"), p1);

12 注解使用

@JsonIgnore:排除属性

@JsonIgnore

private Date birthday;

转换最终的JSON字符串中,不会有birthday键值对。

@JsonFormat:属性值格式化

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date birthday;

转换结果:

{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:23:33”}

13 复杂Java对象转换

List集合转换为JSON字符串:

ObjectMapper mapper = new ObjectMapper();

String json = mapperwriteValueAsString(list);

Systemoutprintln(json);

输出结果:

[{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:25:23”},{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:25:23”},{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:25:23”}]

Map集合转换为JSON字符串

public void test4() throws Exception {

    Person p1 = new Person();

    p1setName("Alex");

    p1setAge(21);

    p1setGender("男");

    p1setBirthday(new Date());

    // 创建JavaBean对象

    Map<String, Object> map = new HashMap<String, Object>();

//        mapput("name", "Alex");

//        mapput("age", "1111");

//        mapput("gender", "xxx");

    mapput("k1", p1);

    mapput("k2", p1);

    mapput("k3", p1);

    // 转换

    ObjectMapper mapper = new ObjectMapper();

    String json = mapperwriteValueAsString(map);

    Systemoutprintln(json);

}

输出结果:

{“k1”:{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:30:08”},“k2”:{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:30:08”},“k3”:{“name”:“Alex”,“age”:21,“gender”:“男”,“birthday”:“2019-03-27 06:30:08”}}

二、JSON ⇒ Java对象

21 JSON转换为Java对象

String json = "{\"name\":\"Alex\",\"age\":21,\"gender\":\"男\",\"birthday\":\"2019-03-27 06:01:54\"}";

ObjectMapper mapper = new ObjectMapper();

Person person = mapperreadValue(json, Personclass);

Systemoutprintln(person);

22 JSON转换为Java集合对象

String json = "[{\"name\":\"Alex\",\"age\":21,\"gender\":\"男\",\"birthday\":\"2019-03-27 06:01:54\"}," +

                "      {\"name\":\"Alex\",\"age\":21,\"gender\":\"男\",\"birthday\":\"2019-03-27 06:01:54\"}," +

                "      {\"name\":\"Alex\",\"age\":21,\"gender\":\"男\",\"birthday\":\"2019-03-27 06:01:54\"}]\n";

ObjectMapper mapper = new ObjectMapper();

List<Person> list = mapperreadValue(json, new TypeReference<List<Person>>() {});

for (Person p : list) {

    Systemoutprintln(p);

}

输出结果:

Person{name=‘Alex’, age=21, gender=‘男’, birthday=Wed Mar 27 14:01:54 CST 2019}

Person{name=‘Alex’, age=21, gender=‘男’, birthday=Wed Mar 27 14:01:54 CST 2019}

Person{name=‘Alex’, age=21, gender=‘男’, birthday=Wed Mar 27 14:01:54 CST 2019}

以上就是关于如何使用jackson反序列化json到对象全部的内容,包括:如何使用jackson反序列化json到对象、Jackson 对象和JSON的相互转换、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9564766.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存