在java中将两个json对象合并为单个对象 [英] merging two json objects into single object in java
本文介绍了在java中将两个json对象合并为单个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个json对象如下:
I have two json objects like below:
{"name":["Karbonn Smart A12 Star (Black & Silver)","Nokia 220 (Black)","Karbonn Smart A52 Plus (Black & Gold)","Karbonn Smart A12 Star (White & Gold)",.......]}
{"price":["Rs. 3,699","Rs. 2,599","Rs. 2,499","Rs. 3,699",..........]}
我想将两个对象结合起来,我尝试使用嵌套为每个循环它没有工作我不知道程序实现这一目标:
I would like to combine both the objects like below I tried by using nested for each loop it did not worked I am not knowing the procedure to achieve this:
{"mobile":[{"name":"Karbonn Smart A12 Star (Black & Silver)","price":"Rs. 2,499"}]...........}
我的代码如下:
for(Element a:mobilename)
{
text= a.text();
arr.add(text);
obj1.put("name", arr);
//a11.add(text);
}
arr2.add(obj1);
for(Element b:price)
{
text1=b.text();
arr1.add(text1);
obj.put("price", arr1);
}
arr2.add(obj1);
arr2.add(obj);
obj2.put("mobile", arr2);
推荐答案
您可以通过for循环迭代并在每次迭代中创建新的JSONObject并将其添加到集合中。最后将该集合添加到mergedObject。例如,
You can iterate via for loop and in every iteration create new JSONObject and add it to a collection. Finally add the collection to the mergedObject. E.g.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
public class Foo {
public static void main(String[] args) throws JSONException {
JSONObject object1 = new JSONObject("{\n" +
" \"name\": [\n" +
" \"Karbonn Smart A12 Star (Black & Silver)\",\n" +
" \"Nokia 220 (Black)\",\n" +
" \"Karbonn Smart A52 Plus (Black & Gold)\",\n" +
" \"Karbonn Smart A12 Star (White & Gold)\",\n" +
" \"Karbonn Smart A50s (Black)\",\n" +
" \"Karbonn Smart A52 Plus (White & Silver)\",\n" +
" \"Karbonn Titanium S2 Plus (White)\",\n" +
" \"Karbonn Smart A11 Star (Black)\",\n" +
" \"Karbonn Smart A11 Star (White)\"\n" +
" ]\n" +
"}");
JSONObject object2 = new JSONObject("{\n" +
" \"price\": [\n" +
" \"Rs. 3,699\",\n" +
" \"Rs. 2,599\",\n" +
" \"Rs. 2,499\",\n" +
" \"Rs. 3,699\",\n" +
" \"Rs. 2,699\",\n" +
" \"Rs. 2,499\",\n" +
" \"Rs. 4,999\",\n" +
" \"Rs. 4,399\",\n" +
" \"Rs. 4,499\"\n" +
" ]\n" +
"}");
JSONArray nameArray = object1.getJSONArray("name");
JSONArray priceArray = object2.getJSONArray("price");
JSONObject mergedObject = new JSONObject("{}");
Collection<JSONObject> collection = new ArrayList<>();
for (int i = 0; i < nameArray.length(); i++) {
JSONObject obj = new JSONObject();
obj.put("name", nameArray.getString(i));
obj.put("price", priceArray.getString(i));
collection.add(obj);
}
mergedObject.put("mobile", collection);
System.out.println(mergedObject);
}
}
输出:
{"mobile":[{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (Black & Silver)"},{"price":"Rs. 2,599","name":"Nokia 220 (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (Black & Gold)"},{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (White & Gold)"},{"price":"Rs. 2,699","name":"Karbonn Smart A50s (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (White & Silver)"},{"price":"Rs. 4,999","name":"Karbonn Titanium S2 Plus (White)"},{"price":"Rs. 4,399","name":"Karbonn Smart A11 Star (Black)"},{"price":"Rs. 4,499","name":"Karbonn Smart A11 Star (White)"}]}
这篇关于在java中将两个json对象合并为单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文