fastjson json中含有\,解析报错

cx6n0qe3  于 3个月前  发布在  其他
关注(0)|答案(3)|浏览(53)

String a = "{"id": 168986793\n" +
" ,"name": "护舒宝瞬洁贴身量多日用\夜用卫生巾5+1片284mm"\n" +
" ,"price": "10"\n" +
" ,"origin_price": "10"\n" +
" ,"minCount": "1"\n" +
" ,"onSale": "1"\n" +
" ,"sku": [ {"id": 181562658\n" +
" ,"name": ""\n" +
" ,"price": "10"\n" +
" ,"origin_price": "10"\n" +
" ,"minCount": 1\n" +
" ,"stock": 0\n" +
" ,"isSellOut": 0\n" +
" }\n" +
" ]\n" +
" }";

Exception in thread "main" com.alibaba.fastjson.JSONException: unclosed string : 夜
at com.alibaba.fastjson.parser.JSONLexerBase.scanString(JSONLexerBase.java:975)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:441)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1327)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1293)
at com.alibaba.fastjson.JSON.parse(JSON.java:137)
at com.alibaba.fastjson.JSON.parse(JSON.java:128)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:201)

用的1.2.24

fcg9iug3

fcg9iug31#

JSONLexerBase
scanString()方法这样改下,貌似就好了

default:
this.ch = ch;
putChar('');
putChar(ch);
//throw new JSONException("unclosed string : " + ch);

aor9mmx1

aor9mmx12#

json格式不对,如果你是在代码字符串里面写,应该是 \\\\, 转义.
String test1 = "{\"id\": 168986793, \"name\": \"护舒宝瞬洁贴身量多日用\\\\,夜用卫生巾5+1片284mm\"}";

文件中测试ok.
test.json

{"name":"护舒宝瞬洁贴身量多日用\\,夜用卫生巾5+1片284mm","id":168986793}
String test2 = Files.readAllLines(Paths.get(new ClassPathResource("test.json").getURI())).get(0);
        System.out.println(JSON.parseObject(test2));

相关问题