如何在JAVA中将一个JSON数组重构为其他格式?

时间:2014-06-20 05:09:58

标签: java json arrays

我有一个 JSON 这样的数组,

JSONArray content=
"[
{"+"\"pageid\":\"19\","+"\"company\":"+"\"C1\","+"\"pageview\":"+"\"10\","+"\"visitfreq\":"+"\"2\"},{"+"\"pageid\":\"19\","+"\"company\":"+"\"C2\","+"\"pageview\":"+"\"20\","+"\"visitfreq\":"+"\"4\"},{"+"\"pageid\":\"200\","+"\"company\":"+"\"C3\","+"\"pageview\":"+"\"30\","+"\"visitfreq\":"+"\"3\"}
]";

JSONArray的代码:

JSONObject jObj1 = new JSONObject();
jObj1.put("pageid", "19");
jObj1.put("company", "C1");
jObj1.put("pageview", "10");
jObj1.put("visitfreq", "2");

JSONObject jObj2 = new JSONObject();
jObj2.put("pageid", "19");
jObj2.put("company", "C2");
jObj2.put("pageview", "20");
jObj2.put("visitfreq", "4");

JSONObject jObj3 = new JSONObject();
jObj3.put("pageid", "200");
jObj3.put("company", "C3");
jObj3.put("pageview", "30");
jObj3.put("visitfreq", "3");

JSONArray jArr = new JSONArray();
jArr.put(jObj1);
jArr.put(jObj2);
jArr.put(jObj3);

Visual representation:

[
    {
        "company": "C1",
        "visitfreq": "2",
        "pageview": "10",
        "pageid": "19"
    },
    {
        "company": "C2",
        "visitfreq": "4",
        "pageview": "20",
        "pageid": "19"
    },
    {
        "company": "C3",
        "visitfreq": "3",
        "pageview": "30",
        "pageid": "200"
    }
]

我已经努力获得下面的输出

这样的出局

[{pageid},[{rest of details}] ,{pageid},[{rest of details}] ]

如果同一个pageid多次出现
它应该是这样的

[{pageid},[{rest of details1},{rest of details2},.. ],{pageid},[{rest of details}] ]

1 个答案:

答案 0 :(得分:0)

您似乎需要创建一个映射Map的{​​{1}}。

获得pageid : [{rest of details1},{rest of details2},.. ]后,您可以将其转换为所需的输出。 以下代码将使用Map(上面代码中的代码)并将其转换为JSONArray

Map<String, JSONArray>

以下是它的样子:

public Map<String, JSONArray> getJsonArrayAsMapOnPageId(JSONArray inputArr) throws JSONException {
    //Map holding pageid : [{company:"",pageview:"",visitfreq:""}, ... , ...] mappings
    Map<String, JSONArray> idMap = new HashMap<String, JSONArray>();

    for (int i=0; i < inputArr.length(); i++) {
        JSONObject inputObj = inputArr.getJSONObject(i);

        String id = inputObj.getString("pageid");

        JSONArray jObjList;
        if (idMap.containsKey(id)) {
            //append to existing list in the Map
            jObjList = idMap.get(id);
        } else {
            //create new list
            jObjList = new JSONArray();
        }

        JSONObject newJsonObj = new JSONObject();
        newJsonObj.put("company", inputObj.get("company"));
        newJsonObj.put("pageview", inputObj.get("pageview"));
        newJsonObj.put("visitfreq", inputObj.get("visitfreq"));

        jObjList.put(newJsonObj);

        idMap.put(id, jObjList);
    }

    return idMap;
}

这是我得到的:

System.out.println("Map looks like:\n" + pageidMap);

我不确定您希望此输出以什么形式(对象,打印到输出等),因此您可以做最后一点将此{ 200=[ { "company": "C3", "visitfreq": "3", "pageview": "30" } ], 19=[ { "company": "C1", "visitfreq": "2", "pageview": "10" }, { "company": "C2", "visitfreq": "4", "pageview": "20" } ] } 转换为{{1没有问题。