合并两个JSONArray数组

 

private static JSONArray joinJSONArray(JSONArray array1, JSONArray array2) {
        StringBuffer sbf = new StringBuffer();
        JSONArray jSONArray = new JSONArray();
        try {
            int len = array1.size();
            for (int i = 0; i < len; i++) {
                JSONObject obj1 = (JSONObject) array1.get(i);
                if (i == len - 1)
                    sbf.append(obj1.toString());
                else
                    sbf.append(obj1.toString()).append(",");
            }
            len = array2.size();
            if (len > 0)
                sbf.append(",");
            for (int i = 0; i < len; i++) {
                JSONObject obj2 = (JSONObject) array2.get(i);
                if (i == len - 1)
                    sbf.append(obj2.toString());
                else
                    sbf.append(obj2.toString()).append(",");
            }
            
            sbf.insert(0, "[").append("]");
            jSONArray = jSONArray.parseArray(sbf.toString());
            return jSONArray;
        } catch (Exception e) {
        }
        return null;
    }

 

你可能感兴趣的:(算法)