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;
}
leo
  • 35
  • 1
  • 1
  • 7
  • "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" **what?** As is this is unclear, maybe you could add some kind of example to illustrate your needs –  Aug 19 '15 at 18:56
  • I have add some more code, can you review it again? thanks. – leo Aug 19 '15 at 19:32
  • Something wrong with `map.get(jsonKey)`? –  Aug 19 '15 at 20:34
  • I tested it and it return my expect result, but I'm not sure if it's fine at all condition, so can you help me review it? thanks – leo Aug 19 '15 at 20:42

1 Answers1

0

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?

Community
  • 1
  • 1
flogy
  • 906
  • 4
  • 16
  • I have write a code and add it to the question, can you give me some advice? thanks. – leo Aug 19 '15 at 19:32
  • when I loop the map(parameter), I am not sure where should I return the JSONObject – leo Aug 19 '15 at 19:36
  • Depends on what you are trying to achieve. As I understood you want to compare the parameters in the map with the json array. If everything is equal, you want to return the json object, if not, return null. Right? I would suggest you to implement it as described in my answer. – flogy Aug 19 '15 at 19:39
  • yes, as you said If everything is equal, you want to return the json object, if not, return null. I am try to implement your described answer, one more question is when I loop the json array, I need put all the field in to the another map? And then compare the two map? – leo Aug 19 '15 at 19:45
  • Exactly. Like that you will have less effort in the actual comparing part. – flogy Aug 19 '15 at 19:49
  • Hi, I found JSONObject is implements Map interface, so I think it can be compare directly? – leo Aug 19 '15 at 19:50
  • Ah, nice! That makes it way easier, give it a try ;) Again, make sure you don't compare the order of the items, if not intended to. – flogy Aug 19 '15 at 19:52
  • I found I am hard to finished this job,can you give me some advice how to make sure every field in map(parameter) is equal and return? – leo Aug 19 '15 at 19:56
  • I write a method and test it ,can you review it if it's correct? – leo Aug 19 '15 at 20:30
  • Looks good to me, except that you are not yet checking the values in the map, but only the keys. I think you do not only want to check if the field names (keys) are equal, but if the field name and field value pairs are all equal (-> key-value pairs). – flogy Aug 20 '15 at 05:33