Fastjson: 两个反斜杠的问题

Created on 27 Jul 2016  ·  5Comments  ·  Source: alibaba/fastjson

fastjson 版本:1.2.14
测试代码

public class CodeTest {
    public static void main(String[] argv) {
        String temp = "{\"option_1\": \"\\u4e0d\\u5403\\u6216\\u5c11\\u4e8e1\\u6b21\"}";
        System.out.println(temp);
        JSONObject object = JSON.parseObject(temp);
        System.out.println(object);
    }
}

输出:

{"option_1": "\u4e0d\u5403\u6216\u5c11\u4e8e1\u6b21"}
{"option_1":"不吃或少于1次"}

问题:
参照上面的结果,很显然,两个反斜杠变成一个,然后与后面的匹配。

All 5 comments

这个不是bug吧

@wenshao 这个我是想保留两个斜杠的。

我认为JSON解析的话,还是最好不要对内容进行修改吧

String temp = "{\"option_1\": \"\\u4e0d\\u5403\\u6216\\u5c11\\u4e8e1\\u6b21\"}";
JSONObject object = JSON.parseObject(temp);
Assert.assertEquals("{\"option_1\":\"不吃或少于1次\"}", JSON.toJSONString(object));
Assert.assertEquals("{\"option_1\":\"\\u4E0D\\u5403\\u6216\\u5C11\\u4E8E1\\u6B21\"}", JSON.toJSONString(object, SerializerFeature.BrowserCompatible));

用SerializerFeature.BrowserCompatible可以得到你要的结果

有什么好的解决方案嘛

Was this page helpful?
0 / 5 - 0 ratings