I have a JSON response like this :
我有这样的JSON响应:
[{"ProjectID":1,"ProjectName":"Test","UserID":[1,3,5,7],"RSID":[2,4,6,8]}]
but I need to merge it like this :
但我需要像这样合并它:
[{"ProjectID":1,"ProjectName":"Test","RS":[{"UserID":1,"RSID":2},{"UserID":3,"RSID":4},{"UserID":5,"RSID":6},{"UserID":7,"RSID":8}
]}]
Is this possible to do?
这可能吗?
4 个解决方案
#1
2
i tried to solve your query in plain javascript. hope it helps you.
http://codepen.io/halimmln/pen/GjjLEW
http://codepen.io/halimmln/pen/GjjLEW
Below code is to perform same task using Java:
下面的代码是使用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());
#2
0
There is no direct method to do that, you just have to read the json and do the logic by yourself.
没有直接的方法可以做到这一点,你只需要阅读json并自己做逻辑。
You can use: https://jsonformatter.curiousconcept.com/ to check the JSON structure , so this would be:
您可以使用: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++)
{
}*/
UPDATE
UPDATE
If the number of UserID
is the same of RSID
just do 1 loop.
如果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
I have not tested it, but something like this should do the trick.
我没有测试过,但是这样的事情应该可以解决。
#3
0
There is no direct way to merge the json as you are restructuring the existing json object. Process the json and set the values as below.
在重构现有的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));
#4
0
try below code
尝试以下代码
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();
}
#1
2
i tried to solve your query in plain javascript. hope it helps you.
http://codepen.io/halimmln/pen/GjjLEW
http://codepen.io/halimmln/pen/GjjLEW
Below code is to perform same task using Java:
下面的代码是使用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());
#2
0
There is no direct method to do that, you just have to read the json and do the logic by yourself.
没有直接的方法可以做到这一点,你只需要阅读json并自己做逻辑。
You can use: https://jsonformatter.curiousconcept.com/ to check the JSON structure , so this would be:
您可以使用: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++)
{
}*/
UPDATE
UPDATE
If the number of UserID
is the same of RSID
just do 1 loop.
如果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
I have not tested it, but something like this should do the trick.
我没有测试过,但是这样的事情应该可以解决。
#3
0
There is no direct way to merge the json as you are restructuring the existing json object. Process the json and set the values as below.
在重构现有的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));
#4
0
try below code
尝试以下代码
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();
}