3

I have created two JSON like following,

First Array:

[
   {
      "id":255,
      "is_new":0,
      "is_checked":true,
      "name":"Towel Rack 650",
      "is_favourite":false
   },
   {
      "id":257,
      "is_new":0,
      "is_checked":true,
      "name":"Towel Rod 450",
      "is_favourite":false
   },
   {
      "id":259,
      "is_new":0,
      "is_checked":true,
      "name":"Napkin Ring - Round",
      "is_favourite":false
   }
]

Second Array:

[
   {
      "id":258,
      "is_new":0,
      "is_checked":false,
      "name":"Towel Rod 650",
      "is_favourite":true
   },
   {
      "id":259,
      "is_new":0,
      "is_checked":false,
      "name":"Napkin Ring - Round",
      "is_favourite":true
   }
]

In that I have to merge both array and also want to keep duplicate values once in final array.

I used following snippet for merging.

private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
        throws JSONException {
    JSONArray result = new JSONArray();
    for (int i = 0; i < arr1.length(); i++) {
        result.put(arr1.get(i));
    }
    for (int i = 0; i < arr2.length(); i++) {
        result.put(arr2.get(i));
    }
    return result;
}

I am getting:

[
   {
      "id":255,
      "is_new":0,
      "is_checked":true,
      "name":"Towel Rack 650",
      "is_favourite":false
   },
   {
      "id":257,
      "is_new":0,
      "is_checked":true,
      "name":"Towel Rod 450",
      "is_favourite":false
   },
   {
      "id":259,
      "is_new":0,
      "is_checked":true,
      "name":"Napkin Ring - Round",
      "is_favourite":false
   },
   {
      "id":258,
      "is_new":0,
      "is_checked":false,
      "name":"Towel Rod 650",
      "is_favourite":true
   },
   {
      "id":259,
      "is_new":0,
      "is_checked":false,
      "name":"Napkin Ring - Round",
      "is_favourite":true
   }
]

In that I am getting duplicate values of id 259 which has different values of is_checked and is_favourite which I want true value for both like:

{
    "id":259,
    "is_new":0,
    "is_checked":true,
    "name":"Napkin Ring - Round",
    "is_favourite":true
}

I have also tried SparseArray but not succeed. Is there any way to do this?

Your help would be appreciated.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

2 Answers2

0

You can add a SparseArray(key:id,value:each JSONObject in your JSONArray) to save your jsonobjects,every get a jsonobject,first check if its id has existed in SparseArray,if not,insert it.or you will do your logic to decide whether to insert it or update it or ignore it.

At last,if you want to get a JSONArray as return type,then add all SparseArray's values to your JSONArray result.

starkshang
  • 8,228
  • 6
  • 41
  • 52
0

Alright, I wrote the code for you:

First, preparing the objects:

        JSONArray arr1 = new JSONArray(),arr2 = new JSONArray(),result = new JSONArray();
        JSONObject jobj = new JSONObject();
        try {
            jobj.put("id", "255");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Towel Rack 650");
            jobj.put("is_favourite", false);
            arr1.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "257");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Towel Rod 450");
            jobj.put("is_favourite", false);
            arr1.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "259");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Napkin Ring - Round");
            jobj.put("is_favourite", false);
            arr1.put(jobj);

            jobj = new JSONObject();
            jobj.put("id", "258");
            jobj.put("is_new", 0);
            jobj.put("is_checked", false);
            jobj.put("name", "Towel Rod 650");
            jobj.put("is_favourite", true);
            arr2.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "259");
            jobj.put("is_new", 0);
            jobj.put("is_checked", false);
            jobj.put("name", "Napkin Ring - Round");
            jobj.put("is_favourite", true);
            arr2.put(jobj);

            result = concatArray(arr1, arr2);

        } catch (JSONException e) {
            e.printStackTrace();
        }

Then, the method:

private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
            throws JSONException {
        JSONArray result = new JSONArray();
        JSONObject jobj1 = new JSONObject(),jobj2 = new JSONObject(),tmpobj = new JSONObject();
        Set<String> objectsList = new HashSet<String>();

        for (int i = 0; i < arr1.length(); i++) {
            if(objectsList.add(arr1.getJSONObject(i).getString("id"))){
                result.put(arr1.getJSONObject(i));
            }

        }
        for (int i = 0; i < arr2.length(); i++) {
            if(objectsList.add(arr2.getJSONObject(i).getString("id"))){
                result.put(arr2.getJSONObject(i));
            } else {
                jobj1 = arr2.getJSONObject(i);
                int index = 0;
                for(int j = 0; j < result.length(); j++){
                    if(result.getJSONObject(j).getString("id").equals(jobj1.getString("id"))){
                        jobj2 = result.getJSONObject(j);
                        index = j;
                    }
                }
                tmpobj.put("id", jobj2.getString("id"));
                tmpobj.put("is_new", jobj2.getInt("is_new"));
                if(jobj1.getBoolean("is_checked")||jobj2.getBoolean("is_checked")){
                    tmpobj.put("is_checked", true);
                } else {
                    tmpobj.put("is_checked", false);
                }
                tmpobj.put("name", jobj2.getString("name"));
                if(jobj1.getBoolean("is_favourite")||jobj2.getBoolean("is_favourite")){
                    tmpobj.put("is_favourite", true);
                } else {
                    tmpobj.put("is_favourite", false);
                }
                result.put(index, tmpobj);
            }
        }
        return result;
    }
M D P
  • 4,525
  • 2
  • 23
  • 35