在Java中,如何组合两个JSON对象数组?

时间:2023-01-24 10:56:41

I have several string each containing a JSON representation of an array of objects. Here's an example in code to illustrate, though this is not my actual code (the JSON strings are passed in):

我有几个字符串,每个字符串包含一个对象数组的JSON表示。这是代码中的一个示例来说明,虽然这不是我的实际代码(传入JSON字符串):

String s1 = "[{name: "Bob", car: "Ford"},{name: "Mary", car: "Fiat"}]";
String s2 = "[{name: "Mack", car: "VW"},{name: "Steve", car: "Mercedes Benz"}]";

I need to combine those two JSON arrays into one large JSON array. I could treat this as a String manipulation problem and replace the inner end square brackets with commas but that's not particularly robust (though I am guaranteed to get valid JSON).

我需要将这两个JSON数组合并为一个大型JSON数组。我可以将其视为一个字符串操作问题,并用逗号替换内端方括号,但这不是特别健壮(尽管我保证获得有效的JSON)。

I'd rather treat these two Strings as JSON arrays and just add them together somehow. It's a great plan except I don't know the "somehow" part.

我宁愿将这两个字符串视为JSON数组,只是以某种方式将它们组合在一起。这是一个很棒的计划,除了我不知道“某种程度”的部分。

Does anyone know a solution in Java that doesn't require constructing Java Object representations of the JSON objects?

有没有人知道Java中的解决方案不需要构建JSON对象的Java Object表示?

Thanks!

谢谢!

5 个解决方案

#1


11  

You really have only two choices: parse the JSON (which invariably would involve constructing the objects) or don't parse the JSON. Not parsing is going to be cheaper, of course.

你真的只有两个选择:解析JSON(总是涉及构造对象)或者不解析JSON。当然,解析不会更便宜。

At first glance your idea about treating it as a String-manipulation problem might sound fragile, but the more I think about it, the more it seems to make fine sense. For error detection you could easily confirm that you were really dealing with arrays by checking for the square brackets; after that, just stripping off the ending bracket, adding a comma, stripping off the beginning bracket, and adding the "tail" should work flawlessly. The only exception I can think of is if either array is empty, you should just return the other String unchanged; again, that's very easy to check for as a String.

乍一看,你将它视为字符串操作问题的想法可能听起来很脆弱,但我想的越多,它看起来就越有意义。对于错误检测,您可以通过检查方括号轻松确认您确实在处理数组;之后,只需剥离结束括号,添加逗号,剥离开始括号,添加“尾巴”应该完美无缺。我能想到的唯一例外是,如果任一数组为空,则应该只返回另一个不变的字符串;再次,这很容易检查为字符串。

I really don't think there's any reason to make it more complex than that.

我真的不认为有任何理由让它变得更复杂。

#2


16  

This code will take sourceArray (s2), and append it to the end of destinationArray (s1):

此代码将获取sourceArray(s2),并将其附加到destinationArray(s1)的末尾:

String s1 = "[{name: \"Bob\", car: \"Ford\"},{name: \"Mary\", car: \"Fiat\"}]";
String s2 = "[{name: \"Mack\", car: \"VW\"},{name: \"Steve\", car: \"Mercedes Benz\"}]";

JSONArray sourceArray = new JSONArray(s2);
JSONArray destinationArray = new JSONArray(s1);

for (int i = 0; i < sourceArray.length(); i++) {
    destinationArray.put(sourceArray.getJSONObject(i));
}

String s3 = destinationArray.toString();

#3


4  

I used this code for Combine two Json Array.

我用这个代码组合了两个Json数组。

String s1 = "[{name: \"Bob\", car: \"Ford\"},{name: \"Mary\", car: \"Fiat\"}]";
String s2 = "[{name: \"Mack\", car: \"VW\"},{name: \"Steve\", car: \"Mercedes Benz\"}]";
String s3=new String("");
s1=s1.substring(s1.indexOf("[")+1, s1.lastIndexOf("]"));
s2=s2.substring(s2.indexOf("[")+1, s2.lastIndexOf("]"));
s3="["+s1+","+s2+"]";
System.out.println(s3);

#4


1  

Use Below Method pass all JSON array in ArrayList this method will return cumulative JsonArray

使用Below Method传递ArrayList中的所有JSON数组,此方法将返回累积的JsonArray

public JSONArray getMergeJson(ArrayList<JSONArray> xyz){
    JSONArray result=null;
    JSONObject obj= new JSONObject();
    obj.put("key",result);
    for(JSONArray tmp:patches){
        for(int i=0;i<tmp.length();i++){
         obj.append("key", tmp.getJSONObject(i));   ;
        }

            }
    return obj.getJSONArray("key");
}

#5


0  

i use this code to append all the elements of a jsonArray to a common JsonArray.

我使用此代码将jsonArray的所有元素追加到公共JsonArray。

public JSONArray getMergeJsonArrays(ArrayList<JSONArray> jsonArrays) throws JSONException
{
    JSONArray MergedJsonArrays= new JSONArray();
     for(JSONArray tmpArray:jsonArrays)
     {
        for(int i=0;i<tmpArray.length();i++)
        {
            MergedJsonArrays.put(tmpArray.get(i));
        }
     }
    return MergedJsonArrays;
}

#1


11  

You really have only two choices: parse the JSON (which invariably would involve constructing the objects) or don't parse the JSON. Not parsing is going to be cheaper, of course.

你真的只有两个选择:解析JSON(总是涉及构造对象)或者不解析JSON。当然,解析不会更便宜。

At first glance your idea about treating it as a String-manipulation problem might sound fragile, but the more I think about it, the more it seems to make fine sense. For error detection you could easily confirm that you were really dealing with arrays by checking for the square brackets; after that, just stripping off the ending bracket, adding a comma, stripping off the beginning bracket, and adding the "tail" should work flawlessly. The only exception I can think of is if either array is empty, you should just return the other String unchanged; again, that's very easy to check for as a String.

乍一看,你将它视为字符串操作问题的想法可能听起来很脆弱,但我想的越多,它看起来就越有意义。对于错误检测,您可以通过检查方括号轻松确认您确实在处理数组;之后,只需剥离结束括号,添加逗号,剥离开始括号,添加“尾巴”应该完美无缺。我能想到的唯一例外是,如果任一数组为空,则应该只返回另一个不变的字符串;再次,这很容易检查为字符串。

I really don't think there's any reason to make it more complex than that.

我真的不认为有任何理由让它变得更复杂。

#2


16  

This code will take sourceArray (s2), and append it to the end of destinationArray (s1):

此代码将获取sourceArray(s2),并将其附加到destinationArray(s1)的末尾:

String s1 = "[{name: \"Bob\", car: \"Ford\"},{name: \"Mary\", car: \"Fiat\"}]";
String s2 = "[{name: \"Mack\", car: \"VW\"},{name: \"Steve\", car: \"Mercedes Benz\"}]";

JSONArray sourceArray = new JSONArray(s2);
JSONArray destinationArray = new JSONArray(s1);

for (int i = 0; i < sourceArray.length(); i++) {
    destinationArray.put(sourceArray.getJSONObject(i));
}

String s3 = destinationArray.toString();

#3


4  

I used this code for Combine two Json Array.

我用这个代码组合了两个Json数组。

String s1 = "[{name: \"Bob\", car: \"Ford\"},{name: \"Mary\", car: \"Fiat\"}]";
String s2 = "[{name: \"Mack\", car: \"VW\"},{name: \"Steve\", car: \"Mercedes Benz\"}]";
String s3=new String("");
s1=s1.substring(s1.indexOf("[")+1, s1.lastIndexOf("]"));
s2=s2.substring(s2.indexOf("[")+1, s2.lastIndexOf("]"));
s3="["+s1+","+s2+"]";
System.out.println(s3);

#4


1  

Use Below Method pass all JSON array in ArrayList this method will return cumulative JsonArray

使用Below Method传递ArrayList中的所有JSON数组,此方法将返回累积的JsonArray

public JSONArray getMergeJson(ArrayList<JSONArray> xyz){
    JSONArray result=null;
    JSONObject obj= new JSONObject();
    obj.put("key",result);
    for(JSONArray tmp:patches){
        for(int i=0;i<tmp.length();i++){
         obj.append("key", tmp.getJSONObject(i));   ;
        }

            }
    return obj.getJSONArray("key");
}

#5


0  

i use this code to append all the elements of a jsonArray to a common JsonArray.

我使用此代码将jsonArray的所有元素追加到公共JsonArray。

public JSONArray getMergeJsonArrays(ArrayList<JSONArray> jsonArrays) throws JSONException
{
    JSONArray MergedJsonArrays= new JSONArray();
     for(JSONArray tmpArray:jsonArrays)
     {
        for(int i=0;i<tmpArray.length();i++)
        {
            MergedJsonArrays.put(tmpArray.get(i));
        }
     }
    return MergedJsonArrays;
}