0

In my project, there is a JSON file like this:

{
"table1": [],
"table2": [{
    "field1": "value1",
    "field2": "value2",
    "field3": "value3"
}],
"table3": []
}

I transfer it to a JSONObject by JSON-Lib. And there is a method to get the child node I have coded as below:

    public static JSONObject getChildNode(JSONObject json, String nodeName, 
    String fieldName1,Object filedValue1, String fieldName2,Object filedValue2) {
    JSONArray jsonArray = (JSONArray) json.get(nodeName);
    JSONObject jsonObject = null;
    for (int i = 0; i < jsonArray.size(); i++) {
        jsonObject = (JSONObject) jsonArray.get(i);
        String value1 = (String) jsonObject.get(fieldName1);
        String value2 = (String) jsonObject.get(fieldName2);
        if (value1.equals(filedValue1) && value2.equals(filedValue2)) {
            return jsonObject;
        }
    }
    return null;
}

Now I want use a map to store the parameters, key is fieldName and value is the field's value as this:

public JSONObject getChildNode(JSONObject json, String nodeName, Map<String, Object> map) {}

Problem is: I don't know how many parameter it will pass, but every value of the Map need to equals the value of jsonArray's value. And finally return the JSONObject I need.

Is there anyone who can help me? Thanks a lot.

I have write a code like below:

    public JSONObject getChildNode(JSONObject json, String nodeName, Map<String, Object> map) {
       JSONArray jsonArray = (JSONArray) json.get(nodeName);
       JSONObject jsonObject,jsonObjectTmp  = null;

       for(int i=0; i<jsonArray.size(); i++) {
            jsonObject = (JSONObject) jsonArray.get(i);

            for (String key : map.keySet()) {
                String jsonKey = (String) jsonObject.get(key);
                if (jsonKey.equals(map.get(key))){
                    jsonObjectTmp = jsonObject;
                }else {
                    jsonObjectTmp = null;
                    break;
                }
            }
        }

        return jsonObjectTmp;
}

but I don't where I should return the JSONObject?

Add code:

    public JSONObject getChildNode(JSONObject json, String nodeName, Map<String, Object> map) {
       JSONArray jsonArray = (JSONArray) json.get(nodeName);
       JSONObject jsonObject = null;
       boolean flag;
       for(int i=0; i<jsonArray.size(); i++) {
            jsonObject = (JSONObject) jsonArray.get(i);
            flag = mapsAreEqual(jsonObject, map);
            if (flag) {
                return jsonObject;
            }
        }
        return null;
}

public static boolean mapsAreEqual(Map<String, Object> mapA, Map<String, Object> mapB) {

    try{
        for (String k : mapB.keySet())
        {

            if (mapA.get(k).hashCode() != mapB.get(k).hashCode()) {
                return false;
            }
        } 
    } catch (NullPointerException np) {
        return false;
    }
    return true;
}
4

1 回答 1

0

就像您已经提到的那样,只需将参数映射传递给 getChildNodeMethod :

public JSONObject getChildNode(JSONObject json, String nodeName, Map<String, Object> map) {}

然后,在第一步中,循环遍历您的 json 数组(就像您已经做的那样)并将条目写入附加映射。在第二步中,您将比较这两个地图。如果不打算这样做,请确保不要比较键值对的顺序。

这是关于如何比较两个映射的另一个问题:比较两个哈希映射是否具有相同的值和相同的键集?

于 2015-08-19T19:05:48.813 回答