Search code examples
javajsondictionaryjson-lib

when use Map<k,v> store field and value, how to get result if parameters is not sure


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;
}

Solution

  • Just pass the map of parameters to the getChildNodeMethod like you already mentioned:

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

    Then, in a first step, loop through your json array (as you already do) and write the entries into an additional map. In a second step you would then compare the two maps. Make sure you don't compare the order of the key-value pairs if not intended to.

    Here is another question on how to compare two maps: Comparing two hashmaps for equal values and same key sets?