Concatenate JSONArrays

前端 未结 3 2009
陌清茗
陌清茗 2021-02-05 05:07

I am using JSONArray under the org.json Package.

My first JSONArray is like:

[[\"249404\",\"VPR249404\"],[\"249403\",\"VPR249403\"],[

相关标签:
3条回答
  • 2021-02-05 05:45

    I would try something like this:

    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 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)):

    private JSONArray concatArray(JSONArray... arrs)
            throws JSONException {
        JSONArray result = new JSONArray();
        for (JSONArray arr : arrs) {
            for (int i = 0; i < arr.length(); i++) {
                result.put(arr.get(i));
            }
        }
        return result;
    }
    
    0 讨论(0)
  • 2021-02-05 05:49

    try this:

    private JSONArray concatArray(@NotNull JSONArray jsArr1, @NotNull JSONArray jsArr2) {
        List<Object> list = jsArr1.toList();
        list.addAll(jsArr2.toList());
        return new JSONArray(list);
    }
    
    0 讨论(0)
  • 2021-02-05 06:04

    Use ...

    jarray1.addAll(jarray2);
    
    0 讨论(0)
提交回复
热议问题