如何在Android中合并两个JSON数组?

时间:2016-09-20 04:41:28

标签: java android json gson

我有这样的JSON响应:

[{"ProjectID":1,"ProjectName":"Test","UserID":[1,3,5,7],"RSID":[2,4,6,8]}]

但我需要像这样合并:

[{"ProjectID":1,"ProjectName":"Test","RS":[{"UserID":1,"RSID":2},{"UserID":3,"RSID":4},{"UserID":5,"RSID":6},{"UserID":7,"RSID":8}
]}]

这可能吗?

4 个答案:

答案 0 :(得分:2)

i tried to solve your query in plain javascript. hope it helps you.

http://codepen.io/halimmln/pen/GjjLEW

下面的代码是使用Java执行相同的任务:

 JSONParser parser = new JSONParser();
    String oldJSON = "[{\"ProjectID\":1,\"ProjectName\":\"Test\",\"UserID\":[1,3,5,7],\"RSID\":[2,4,6,8]}]";

    JSONObject newJSON = new JSONObject();
    Object obj = parser.parse(oldJSON);
    JSONArray mainArray = (JSONArray) obj;
    JSONObject objects = (JSONObject) mainArray.get(0);

    newJSON.put("ProjectID", objects.get("ProjectID"));
    newJSON.put("ProjectName", objects.get("ProjectName"));

    JSONArray userId = (JSONArray) objects.get("UserID");
    JSONArray rsid = (JSONArray) objects.get("RSID");
    JSONArray newArr = new JSONArray();

    for (int i = 0; i < userId.size(); i++) {
        JSONObject combine = new JSONObject();
        System.out.println("inside array" + userId.get(i));
        combine.put("UserID", userId.get(i));
        combine.put("RSID", rsid.get(i));
        newArr.add(combine);
    }
    newJSON.put("RS", newArr);
    System.out.println("json" + newJSON.toJSONString());

答案 1 :(得分:0)

没有直接的方法可以做到这一点,你只需要阅读json并自己做逻辑。

您可以使用:https://jsonformatter.curiousconcept.com/来检查JSON结构,所以这将是:

jsonArray = getJSONArray(response.toString()) // the full response; 
jsonObject = jsonArray.getJSONObject(0);
String myProjectID = jsonObject.getString("ProjectID"); 
String myProjectName=jsonObject.getString("ProjectName"); 
  /* userIdArray = jsonObject .getJSONArray("UserID");
 for (int i = 0; i < userIdArray .length(); i++)
{
  //javaArray pushing values JSONObject jsonO = userIdArray .getJSONObject(i);
}

rsIdArray = jsonObject.getJSONArray("RSID");
for (int i = 0; i < rsIdArray .length(); i++)
{

}*/

<强>更新

如果UserID的数量与RSID相同,则只需执行1次循环。

 userIdArray = jsonObject .getJSONArray("UserID");
 rsIdArray = jsonObject.getJSONArray("RSID");

for (int i = 0; i < userIdArray .length(); i++)
    {
      //javaArray pushing values JSONObject jsonO = userIdArray .getJSONObject(i);
//javaArray pushing values JSONObject jsonO = rsIdArray.getJSONObject(i);
    }




 //new JSON 

JSONObject jsonNew = new JSONObject();
jsonNew .put("myProjectID ", myProjectID );
jsonNew .put("myProjectName", myProjectName);
JSONArray myNewArray = new JSONArray();
 for (int i = 0; i < newArrayUser.length(); i++)
{
JSONObject jobj= new JSONObject();
jobj.put( "UserID", newArrayUser[i] );
jobj.put( "RSID", newArrayRSID[i]);
myNewArray.put(jobj);
 }
jsonNew.put(myNewArray);

 //all values

我还没有测试过,但这样的事情应该可以解决问题。

答案 2 :(得分:0)

在重构现有的json对象时,没有直接的方法来合并json。处理json并设置如下的值。

  var myObj = {"ProjectID":1,"ProjectName":"Test","UserID":[1,3,5,7],"RSID":[2,4,6,8]}
    var myArray = [];
     var RS = {};
        var rsuserObj ={};
        for(var key in myObj.UserID){
           rsuserObj["userID"] = myObj.UserID[key];
           rsuserObj["RSID"] = myObj.RSID[key];
           myArray.push(rsuserObj);

        }

    var updatedObj = {"ProjectID":1,"ProjectName":"Test", "RS": myArray};

    console.log(JSON.stringify(updatedObj));

答案 3 :(得分:0)

尝试以下代码

 try {
        String json1 = "[{\"ProjectID\":1,\"ProjectName\":\"Test\",\"UserID\":[1,3,5,7],\"RSID\":[2,4,6,8]}]";
        JSONArray jsonArray1 = new JSONArray(json1);

        JSONObject jsonObject = jsonArray1.optJSONObject(0);
        String key1 = "UserID";
        String key2 = "RSID";
        String keyFinal = "RS";
        JSONArray userIDArray = jsonObject.optJSONArray("UserID");
        JSONArray rsIDArray = jsonObject.optJSONArray(key2);
        jsonObject.remove(key1);
        jsonObject.remove(key2);

        int index = 0;

        JSONArray rsFinalArray = new JSONArray();

        if (userIDArray.length() > rsIDArray.length()) {
            index = userIDArray.length();
        } else {
            index = rsIDArray.length();
        }


        for (int i = 0; i < index; i++) {
            JSONObject rsObject = new JSONObject();
            rsObject.accumulate(key1, userIDArray.opt(i));
            rsObject.accumulate(key2, rsIDArray.opt(i));
            rsFinalArray.put(rsObject);
        }

        jsonObject.accumulate(keyFinal, rsFinalArray);
        JSONArray jsonArrayFinal = new JSONArray();
        jsonArrayFinal.put(jsonObject);
        Log.v("TAG", "result :" + jsonArrayFinal.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }