JSON格式转换

public enum BookingType {
    CR
}

@ToString
@Slf4j
@Getter
@Setter
public class Demo {
    private String st;
    private BookingType type;
    private String sd;

    @Test
    public void run() {
        //json字符串少一个字段
        String c = "{ \"st\":\"tt\",\"type\":\"CR\"}";
        Demo demo1 = JSON.parseObject(c, Demo.class);
        log.info(demo1.toString());
        //json字符串少一个字段,多一个其它字段
        String c4 = "{ \"st\":\"tt\",\"type\":\"CR\",\"sc\":12}";
        Demo demo4 = JSON.parseObject(c4, Demo.class);
        log.info(demo4.toString());
        //json字符串多一个字段
        String c3 = "{ \"st\":\"tt\",\"type\":\"CR\",\"sd\":12,\"sc\":12}";
        Demo demo3 = JSON.parseObject(c3, Demo.class);
        log.info(demo3.toString());
        //json字符串的内容,不在字段定义的枚举类
        String c2 = "{ \"st\":\"tt\",\"type\":\"xx\"}";
        Demo demo2 = JSON.parseObject(c2, Demo.class);
        log.info(demo2.toString());
    }
}
控制台输出:
Demo(st=tt, type=CR, sd=null)
Demo(st=tt, type=CR, sd=null)
Demo(st=tt, type=CR, sd=12)

com.alibaba.fastjson.JSONException: No enum constant com.wework.spacecore.enums.BookingType.xx

    at com.alibaba.fastjson.parser.deserializer.EnumDeserializer.deserialze(EnumDeserializer.java:75)
    at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:33)
    at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:75)
    at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer$InnerJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:87)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:331)
    at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:101)
    at Fastjson_ASM_Demo_1.deserialze(Unknown Source)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:556)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:251)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:186)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:304)
    at com.wework.spacecore.Demo.run(Demo.java:37)
    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:498)
    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.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

你可能感兴趣的:(JSON格式转换)