fastjson 如果javaObject里属性是boolean值,且json末尾有“\n”时候parseObject会报异常

svgewumm  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(295)

dto属性是boolean同时jsonstring结尾有\n会出现如下异常
解决方法:
1、dto属性采用boolean的 Package 类
2、jsonstring去掉结尾的"\n"
3、jsonstring采用字符串"false"去掉false

public class TestObj{
    private b oolean res = false;

    public boolean isRes() {
        return res;
    }

    public void setRes(boolean res) {
        this.res = res;
    }

    public static void main(String[] args) {
        System.out.println(JSON.parseObject("{\"res\":false}\n",TestObj.class));
    }

}```

异常如下:
Exception in thread "main" com.alibaba.fastjson.JSONException: unclosed.str
at com.alibaba.fastjson.parser.JSONLexerBase.scanSymbol(JSONLexerBase.java:645)
at com.alibaba.fastjson.parser.JSONLexerBase.scanSymbol(JSONLexerBase.java:591)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:442)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:832)
at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_TestObj.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:185)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:621)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:339)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:243)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:456)
at com.xiaojukeji.sec.mcs.api.common.TestObj.main(TestObj.java:22)

相关问题