fxleyu / cu-cafes

This is a repository of Java. And CU is a cafes unders downstairs.
2 stars 0 forks source link

JSON 序列号异常的问题 #182

Open fxleyu opened 4 months ago

fxleyu commented 4 months ago
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;

public class JsonTest {

    @Test
    public void test() {
        Map<String, String> strMap = new HashMap<>();
        strMap.put("1", "one");

        List<Map<String, String>> list = Lists.newArrayList(strMap, strMap, strMap, strMap);
        System.out.println(JSON.toJSONString(list));

        System.out.println(JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect));
    }
}

打印结果

[{"1":"one"},{"$ref":"$[0]"},{"$ref":"$[0]"},{"$ref":"$[0]"}]
[{"1":"one"},{"1":"one"},{"1":"one"},{"1":"one"}]