fastjson 反序列化出错

byqmnocz  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(304)
private final Type zoneGroupListRef = new TypeReference<Map<String, List<String>>>() {
        }.getType();

    @Test
    public void parseJson() {
        String s = "{\"idc1\":[\"default\"],\"idc2\":[\"default\"]}";

        Map<String, List<String>> zoneGroupMap = JSON.parseObject(s, zoneGroupListRef);
        System.out.println(zoneGroupMap.keySet());
    }

代码里是上面这样解析的,但是会偶尔抛出下面的解析失败异常。字符串的长度根本就没有 49 。

com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 49, json : "{\"idc1\":[\"default\"],\"idc2\":[\"default\"]}"
        at com.alibaba.fastjson.parser.deserializer.MapDeserializer.parseMap(MapDeserializer.java:94)
        at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:59)
        at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:41)
        at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:333)
        at com.alibaba.fastjson.JSON.parseObject(JSON.java:296)

相关问题