本篇文章给大家分享的是有关json串和实体类字段不一致如何解决,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
首先,我们建立一个实体类:这里简单定义了name,sex,age三个属性,以及get set tostring方法。
public class Student { private String name; private String sex; private Integer age; public String getName() { return name; } public String getSex() { return sex; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
当字段少于实体类字段:正常通过,没有的字段会被赋予默认值
//fastjson 少字段 @Test public void testFastjson01() { String jsonStr = "{\"age\":18,\"name\":\"zhangsan\"}"; Student stu = JSON.parseObject(jsonStr, Student.class); System.out.println(stu); // Student [name=zhangsan, sex=null, age=18] }
当字段多于实体类字段:正常通过,没有的字段不会被反序列化到对象中
//fastjson 多字段 @Test public void testFastjson02() { String jsonStr = "{\"age\":20,\"name\":\"lisi\",\"sex\":\"男\",\"hobby\":\"basketball\"}"; Student stu = JSON.parseObject(jsonStr, Student.class); System.out.println(stu); // Student [name=lisi, sex=男, age=20] }
当字段少于实体类字段:正常通过,没有的字段会被赋予默认值
//jackson 少字段 @Test public void testJackson01() throws Exception{ String jsonStr = "{\"age\":18,\"name\":\"zhangsan\"}"; ObjectMapper om = new ObjectMapper(); Student stu = om.readValue(jsonStr, Student.class); System.out.println(stu); // Student [name=zhangsan, sex=null, age=18] }
当字段多于实体类字段:
//jackson 多字段 @Test public void testJackson02() throws Exception{ String jsonStr = "{\"age\":20,\"name\":\"lisi\",\"sex\":\"男\",\"hobby\":\"basketball\"}"; ObjectMapper om = new ObjectMapper(); Student stu = om.readValue(jsonStr, Student.class); System.out.println(stu); }
此时会报错,这个错翻译过来就是,一个无法识别的field。
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "hobby" (class com.cyj.demo03.Student), not marked as ignorable (3 known properties: "name", "sex", "age"]) at [Source: {"age":20,"name":"lisi","sex":"男","hobby":"basketball"}; line: 1, column: 44] (through reference chain: com.cyj.demo03.Student["hobby"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51) at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:817) at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:958) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1324) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1302) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:249) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3564) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2580) at com.cyj.demo03.TestJson.testJackson02(TestJson.java:40) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
由此可见,在默认情况下,jackson是不支持json串的字段多于实体类字段的,但是,jackson也提供了解决方案,我们在实体类上,添加注解@JsonIgnoreProperties,这个注解有一个ignoreUnknown属性,默认值为false,我们可以将它改为true,这样jackson在反序列化的时候,就会忽略掉不存在的属性了。(@JsonIgnoreProperties(ignoreUnknown = true))
补充:json字段名不一致的问题
项目开发过程中,有时候可能要对接第三方平台,可能就会存在字段名不一样的问题,而双方的字段名不一致, 而代码开发已经有一段时间勒,如果就因为字段名不一样的问题,双方改成属性名一致,这样改动量太大,也不保险,下面我来接手一下,针对这种场景,如果解决。
假设: 系统通过http对接,传的是json。有一个参数"名称"
第三方平台:name
开发环境:userName
直接通过实体类映射,不用说肯定有问题
@JsonProperty("name") private String userName;
以上就是json串和实体类字段不一致如何解决,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。