Skip to content Skip to sidebar Skip to footer

Concatenate Jsonarrays

I am using JSONArray under the org.json Package. My first JSONArray is like: [['249404','VPR249404'],['249403','VPR249403'],['249391','M249391']] and Second [['249386','M249386'

Solution 1:

I would try something like this:

privateJSONArrayconcatArray(JSONArray arr1, JSONArray arr2)
        throws JSONException {
    JSONArray result = newJSONArray();
    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 don't have a compiler right now to test, but you can give it a try and see if it works (or, at least, it gives you an idea of how to do it).

EDIT

This version could concat multiple arrays (concatArray(arr1, arr2, arr3)):

privateJSONArrayconcatArray(JSONArray... arrs)
        throws JSONException {
    JSONArray result = newJSONArray();
    for (JSONArray arr : arrs) {
        for (int i = 0; i < arr.length(); i++) {
            result.put(arr.get(i));
        }
    }
    return result;
}

Solution 2:

try this:

privateJSONArrayconcatArray(@NotNull JSONArray jsArr1, @NotNull JSONArray jsArr2) {
    List<Object> list = jsArr1.toList();
    list.addAll(jsArr2.toList());
    returnnewJSONArray(list);
}

Solution 3:

Use ...

jarray1.addAll(jarray2);

Post a Comment for "Concatenate Jsonarrays"