Android:JSON自定义合并

时间:2015-11-07 11:48:22

标签: android arrays json

我创建了两个JSON,如下所示,

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
   }
]

第二阵列:

[
   {
      "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
   }
]

因为我必须合并两个数组,并希望在最终数组中保留一次重复值。

我使用以下snippet进行合并。

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;
}

我得到了:

[
   {
      "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
   }
]

因为我得到 id 259 的重复值,其中我想要is_checkedis_favourite的值不同两者的true值如下:

{
    "id":259,
    "is_new":0,
    "is_checked":true,
    "name":"Napkin Ring - Round",
    "is_favourite":true
}

我也试过SparseArray但没有成功。有没有办法做到这一点?

我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以添加SparseArray(键:id,值:JSONArray中的每个JSONObject)来保存您的jsonobjects,每次获取一个jsonobject,首先检查它的id是否存在于SparseArray中,如果没有,请插入它。或者你将做出决定是插入它还是更新或忽略它的逻辑。

最后,如果要将JSONArray作为返回类型,则将所有SparseArray值添加到JSONArray结果中。

答案 1 :(得分:0)

好吧,我为你写了代码:

首先,准备对象:

        JSONArray arr1 = new JSONArray(),arr2 = new JSONArray(),result = new JSONArray();
        JSONObject jobj = new JSONObject();
        try {
            jobj.put("id", "255");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Towel Rack 650");
            jobj.put("is_favourite", false);
            arr1.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "257");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Towel Rod 450");
            jobj.put("is_favourite", false);
            arr1.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "259");
            jobj.put("is_new", 0);
            jobj.put("is_checked", true);
            jobj.put("name", "Napkin Ring - Round");
            jobj.put("is_favourite", false);
            arr1.put(jobj);

            jobj = new JSONObject();
            jobj.put("id", "258");
            jobj.put("is_new", 0);
            jobj.put("is_checked", false);
            jobj.put("name", "Towel Rod 650");
            jobj.put("is_favourite", true);
            arr2.put(jobj);
            jobj = new JSONObject();
            jobj.put("id", "259");
            jobj.put("is_new", 0);
            jobj.put("is_checked", false);
            jobj.put("name", "Napkin Ring - Round");
            jobj.put("is_favourite", true);
            arr2.put(jobj);

            result = concatArray(arr1, arr2);

        } catch (JSONException e) {
            e.printStackTrace();
        }

然后,方法:

private JSONArray concatArray(JSONArray arr1, JSONArray arr2)
            throws JSONException {
        JSONArray result = new JSONArray();
        JSONObject jobj1 = new JSONObject(),jobj2 = new JSONObject(),tmpobj = new JSONObject();
        Set<String> objectsList = new HashSet<String>();

        for (int i = 0; i < arr1.length(); i++) {
            if(objectsList.add(arr1.getJSONObject(i).getString("id"))){
                result.put(arr1.getJSONObject(i));
            }

        }
        for (int i = 0; i < arr2.length(); i++) {
            if(objectsList.add(arr2.getJSONObject(i).getString("id"))){
                result.put(arr2.getJSONObject(i));
            } else {
                jobj1 = arr2.getJSONObject(i);
                int index = 0;
                for(int j = 0; j < result.length(); j++){
                    if(result.getJSONObject(j).getString("id").equals(jobj1.getString("id"))){
                        jobj2 = result.getJSONObject(j);
                        index = j;
                    }
                }
                tmpobj.put("id", jobj2.getString("id"));
                tmpobj.put("is_new", jobj2.getInt("is_new"));
                if(jobj1.getBoolean("is_checked")||jobj2.getBoolean("is_checked")){
                    tmpobj.put("is_checked", true);
                } else {
                    tmpobj.put("is_checked", false);
                }
                tmpobj.put("name", jobj2.getString("name"));
                if(jobj1.getBoolean("is_favourite")||jobj2.getBoolean("is_favourite")){
                    tmpobj.put("is_favourite", true);
                } else {
                    tmpobj.put("is_favourite", false);
                }
                result.put(index, tmpobj);
            }
        }
        return result;
    }