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.