解决FastJson循环引用的问题

temp 本来被循环引用,运行方法报错。
解决方法:对temp进行处理

SerializerFeature feature = SerializerFeature.DisableCircularReferenceDetect; temp = JSONObject.parseObject(JSON.toJSONString(temp,feature));

以下为情景复现

public JSONObject convertQueryGroupInterface(JSONObject querygroup) {

    JSONArray group = (JSONArray) querygroup.get("treeNodes");
    JSONArray convert = new JSONArray();
    JSONObject temp = new JSONObject();
    JSONObject temp1  = new JSONObject();
    SerializerFeature feature = SerializerFeature.DisableCircularReferenceDetect;
    JSONObject result = null;

    for (int i = 0; i <group.size(); i++){
        temp1 = (JSONObject) group.get(i);
        if (group != null){
            temp = JSONObject.parseObject(JSON.toJSONString(temp,feature));//解决FastJson循环引用的问题
            temp.put("nodeId", temp1.get("groupid"));
            temp.put("nodeType", temp1.get("nodeType"));
            temp.put("nodeName", temp1.get("nodeName"));
            temp.put("isLeaf", false);
            temp.put("expanded", false);
            temp.put("checkStatus",temp1.get("groupstatus"));
        }

        convert.add(i,temp);
        System.out.println("convert内容:"+convert);
    }

    for (int i = 0 ; i < convert.size() ; i ++){
        result = convert.getJSONObject(i);
    }

    return result;
}
posted @ 2020-08-02 07:33  初君  阅读(325)  评论(0编辑  收藏  举报