开发者社区 问答 正文

not allow unquoted fieldName, offset 2, character

2.0.9 没有引号的json还是无法反序列化

` JSONObject json1 = JSON.parseObject("{code:1234}");

System.out.println(json1);

JSONObject json2 = JSONObject.parseObject("{code:1234}");

System.out.println(json2); `

报错:

` Exception in thread "main" com.alibaba.fastjson2.JSONException: not allow unquoted fieldName, offset 2, character c, line 1,

column 3, fastjson-version 2.0.9 {code:1234}

at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1378)

at com.alibaba.fastjson2.JSON.parseObject(JSON.java:142)

at test.Test.main(Test.java:21)

原提问者GitHub用户woniudiancang

展开
收起
飘飘斯嘉丽 2023-04-21 11:25:10 1092 分享 版权
1 条回答
写回答
取消 提交回答
  • JSONObject json1 = JSON.parseObject("{code:1234}",JSONReader.Feature.AllowUnQuotedFieldNames); System.out.println(json1 ); JSONObject json2 = JSONObject.parseObject("{code:1234}",JSONObject.class,JSONReader.Feature.AllowUnQuotedFieldNames); System.out.println(json2);

    JSON.config这个方法是全局配置的,true就是打开,false就是关闭。

    原回答者GitHub用户wsxe9988

    2023-04-21 14:32:34
    赞同 展开评论
问答分类:
问答地址: