Jackson是Spring Boot默认的JSON数据处理框架,然而其并不依赖于任何的Spring 库。有的小伙伴认为Jackson只能在Spring框架内应用,其实不是的,没有这种限度。它提供了很多的JSON数据处理办法、注解,也包含流式API、树模型、数据绑定,以及简单数据类型转换等性能。它尽管简略易用,但相对不是小玩具,本节为大家介绍Jackson的根底外围用法,更多的内容我会写成一个系列,5-10篇文章,请您持续关注我。
在 《序列化与反序列化外围用法-JSON框架Jackson精解第一篇》 也就是上一篇中,为大家介绍了这些内容
- 一、根底筹备
- 二、序列化办法
- 三、反序列化办法
- 四、字段重命名
@JsonProperty
- 五、疏忽null字段的序列化
@JsonInclude
- 六、疏忽指定的字段
本篇文章中为大家介绍,一些非凡JOSN数据格式解决-JSON框架Jackson精解第2篇:
- 一、从URL读取JSON数据
- 二、Unknow Properties 赋值失败解决
- 三、未赋值Java Bean序列化
- 四、日期格式化
一、从URL读取JSON数据
Jackson不仅能够将字符串反序列化为 Java POJO对象,还能够申请近程的API,取得近程服务的JSON响应后果,并将其转换为Java POJO对象。
@Testvoid testURL() throws IOException { URL url = new URL("https://jsonplaceholder.typicode.com/posts/1"); //近程服务URL ObjectMapper mapper = new ObjectMapper(); //从URL获取JSON响应数据,并反序列化为java 对象 PostDTO postDTO = mapper.readValue(url, PostDTO.class); System.out.println(postDTO);}
jsonplaceholder.typicode.com
是一个收费提供HTTP测试服务的网站,咱们能够利用它进行测试- 近程服务API返回后果是一个JSON字符串,一篇post稿件蕴含userId,id,title,content属性
- PostDTO 是咱们本人定义的java 类,同样蕴含userId,id,title,content成员变量
下文是控制台打印输出后果,postDTO的toString()办法输入。
PostDTO(userId=1, id=1, title=sunt aut facere repellat provident occaecati excepturi optio reprehenderit, body=quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto)
二、Unknow Properties 赋值失败解决
有的时候,客户端提供的JSON字符串属性,多于咱们服务端定义的java 类的成员变量。
比方上图中的两个类,
- 咱们先将PlayerStar序列化为JSON字符串,蕴含age属性
- 而后将JSON字符串转换为PlayerStar2,不蕴含age属性
@Testvoid testUnknowProperties() throws IOException { ObjectMapper mapper = new ObjectMapper(); PlayerStar player = PlayerStar.getInstance(); //为PlayerStar 各属性赋值,能够参考本系列文章第一篇 //将PlayerStar序列化为JSON字符串 String jsonString = mapper.writeValueAsString(player); System.out.println(jsonString); //将JSON字符串反序列化为PlayerStar2对象 PlayerStar2 player2 = mapper.readValue(jsonString, PlayerStar2.class); System.out.println(player2);}
当进行反序列化的时候,会抛出上面的异样。这是因为JSON字符串所蕴含的属性,多余Java类的定义(多出一个阿age,赋值时找不到setAge办法)。
{"age":45,"playerName":"乔丹"}com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "age" (class com.example.demo.javabase.PlayerStar2), not marked as ignorable (one known property: "playerName"]) at [Source: (String)"{"age":45,"playerName":"乔丹"}"; line: 1, column: 10] (through reference chain: com.example.demo.javabase.PlayerStar2["age"])
如果咱们想疏忽掉age属性,不承受咱们的java类未定义的成员变量数据,能够应用上面的办法,就不会抛出UnrecognizedPropertyException了。
ObjectMapper mapper = new ObjectMapper();mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
三、未赋值Java Bean序列化
有的时候,咱们明晓得某些类的数据可能为空,咱们通常也不会为它赋值。然而客户端就是须要这个{}
的JSON对象,咱们该怎么做?
public class MyEmptyObject { private Integer i; //没有get set办法}
咱们能够为ObjectMapper设置disable序列化个性:FAIL_ON_EMPTY_BEANS,也就是容许对象的所有属性均未赋值。
@Testvoid testEmpty() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); String jsonString = mapper.writeValueAsString(new MyEmptyObject()); System.out.println(jsonString);}
默认状况下不做设置,会抛出上面的异样InvalidDefinitionException。设置disable序列化个性:FAIL_ON_EMPTY_BEANS之后,会序列化为{}
字符串。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.demo.jackson.JacksonTest1$MyEmptyObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
四、日期格式化
日期格式化,是咱们JSON序列化与反序列化过程中比拟常见的需要
ObjectMapper mapper = new ObjectMapper();Map temp = new HashMap();temp.put("now", new Date());String s = mapper.writeValueAsString(temp);System.out.println(s);
默认状况下,针对java中的日期及相干类型,Jackson的序列化后果如下
{"now":1600564582571}
如果咱们心愿在JSON序列化及反序列化过程中,日期格式化,须要做如下的解决
ObjectMapper mapper = new ObjectMapper();mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); //留神这里mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); //留神这里Map temp = new HashMap();temp.put("now", new Date());String s = mapper.writeValueAsString(temp);System.out.println(s);
控制台打印输出后果如下:
{"now":"2020-09-20"}
欢送关注我的博客,外面有很多精品合集
本文转载注明出处(必须带连贯,不能只转文字):字母哥博客 - zimug.com