欢迎光临
感受代码之美

阿里巴巴fastjson使用示例

logo.jpg

官方维基:新手指南

示例数据1:

String jsonString = 
{
    "edges": {
        "oim-6i8-1lp1-6h4": {
            "id": "oim-6i8-1lp1-6h4",
            "label": "link.legalperson",
            "linkName": "法人关系",
            "source": "8432",
            "target": "8392",
            "propertyList": [
                {
                    "propertyName": "数据来源ID",
                    "propertyKey": "dataSourcesId",
                    "propertyValue": "630cfb50b0c74ba6bb37038a2da545d6",
                    "statistics": true,
                    "timeline": false
                },
                {
                    "propertyName": "数据来源类型",
                    "propertyKey": "dataSourcesType",
                    "propertyValue": "MA",
                    "statistics": true,
                    "timeline": false
                }
            ]
        }
    },
    "edgesMeta": {
        "link.legalperson": {
            "color": "050ff6",
            "labelName": "法人关系"
        }
    },
    "nodes": {
        "8392": {
            "id": "8392",
            "label": "object.enterprise",
            "objectName": "振德医疗用品股份有限公司",
            "extObjectProMap": {},
            "propertyList": []
        },
        "8432": {
            "id": "8432",
            "label": "object.person",
            "objectName": "张某",
            "extObjectProMap": {},
            "propertyList": []
        }
    },
    "nodesMeta": {
        "object.enterprise": {
            "iconColor": "ebf1fc",
            "icon": "F0F7",
            "backgroundColor": "7076ed",
            "labelName": "单位",
            "category": "entity"
        },
        "object.person": {
            "iconColor": "ebf1fc",
            "icon": "F183",
            "backgroundColor": "7076ed",
            "labelName": "人员",
            "category": "entity"
        }
    },
    "paths": [
        {
            "edgeIds": [
                "oim-6i8-1lp1-6h4"
            ],
            "nodeIds": [
                "8392",
                "8432"
            ]
        }
    ],
    "sourceNode": "8432",
    "targetNode": "8392"
}

处理代码1:

// 1.json字符串转换为JSONObject
JSONObject jsonObject = (JSONObject) JSON.parse(jsonString);


// 2.获取某一元素对应值(比如edges)
JSONObject edges = (JSONObject) jsonObject.get("edges");

// 3.JSONObject转换为Map
Map<String, Object> edgesMap = JSONObject.toJavaObject(edges, Map.class); 

// 4.遍历map
Iterator<Map.Entry<String, Object>> edgesIterator = edgesMap.entrySet().iterator();
while (edgesIterator.hasNext()) {
    Map.Entry<String, Object> entry = edgesIterator.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

示例数据2:

{
    "edgesMeta": {
        "link.legalperson": {
            "color": "050ff6",
            "labelName": "法人关系"
        }
    },
    "nodes": [
        {
            "id": "8392",
            "label": "object.enterprise",
            "objectName": "振德医疗用品股份有限公司",
            "extObjectProMap": {},
            "propertyList": []
        }
    ],
    "edges": [
        {
            "id": "oim-6i8-1lp1-6h4",
            "label": "link.legalperson",
            "linkName": "法人关系",
            "source": "8432",
            "target": "8392",
            "propertyList": [
                {
                    "propertyName": "数据来源ID",
                    "propertyKey": "dataSourcesId",
                    "propertyValue": "630cfb50b0c74ba6bb37038a2da545d6",
                    "statistics": true,
                    "timeline": false
                },
                {
                    "propertyName": "数据来源类型",
                    "propertyKey": "dataSourcesType",
                    "propertyValue": "MA",
                    "statistics": true,
                    "timeline": false
                }
            ]
        }
    ],
    "nodesMeta": {
        "object.enterprise": {
            "iconColor": "ebf1fc",
            "icon": "F0F7",
            "backgroundColor": "7076ed",
            "labelName": "单位",
            "category": "entity"
        }
    }
}

处理代码2:

// 1.json字符串转换为JSONObject
JSONObject jsonObject = (JSONObject) JSON.parse(jsonString);

// 2.获取某一元素对应值(比如edges),并转换为JSONArray
JSONArray edges = (JSONArray) jsonObject.get("edges");

// 3.JSONArray转换为字符串
String edgesStr = JSONArray.toJSONString(edges);

// 4.json字符串转换为List
List<Map> edgesList = JSONArray.parseArray(edgesStr, Map.class);
转载请注明来源:四个空格 » 阿里巴巴fastjson使用示例

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址