0

I am working on android studio.

I have two json arrays such as...

jsonArray1:

[
  {"file":"ay.m4a", "version":"5"}, 
  {"file":"kt.m4a", "version":"5"}
]

jsonArray2:

[
  {"file":"hh.m4a", "version":"7"}, 
  {"file":"sh.m4a", "version":"7"}
]

Then, I want to merge these arrays like this...

jsonArrayMerged:

[
  {"file":"ay.m4a", "version":"5"}, 
  {"file":"kt.m4a", "version":"5"}, 
  {"file":"hh.m4a", "version":"7"}, 
  {"file":"sh.m4a", "version":"7"}
]

How can I do this?

3
  • can't just instantiate two JSONArray and then add the contents of one to another? Commented Sep 22, 2017 at 9:26
  • create one new array and retrieve an object from your previous arrays and put into a newly created array Commented Sep 22, 2017 at 9:27
  • Try this link (stackoverflow.com/questions/7940711/…) Commented Sep 22, 2017 at 9:31

1 Answer 1

6

Try below code, This will give your desired result.

JSONArray jsonArray = new JSONArray();
JSONArray jsonArray1 = new JSONArray();
try {
    for (int i = 0; i < jsonArray1.length(); i++) {
        JSONObject jsonObject = jsonArray1.getJSONObject(i);
        jsonArray.put(jsonObject);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Now use jsonArray which is having all merged jsonObject

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.