英文:
How to merge two JsonArray?
问题
以下是翻译好的内容:
我发现所有关于同一个问题的答案都涉及到 org.json.JSONArray
。我需要一个针对 javax.json.JsonArray 的解决方案。
我有两个 JSON 数组,当我尝试使用 array1.addAll(array2)
来合并它们时,我会得到一个 java.lang.UnsupportedOperationException
。
...
JsonObject users = getUsers(searchBase, filter, (String[]) null);
...
final JsonObject guests = getUsers(searchBase, filter, (String[]) null);
users.getJsonArray("Resources").addAll(guests.getJsonArray("Resources"));
...
我获得的 JsonObjects
(users 和 guests)都具有以下形式:
{
"startIndex": 1,
"totalResults": 7,
"itemsPerPage": 7,
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
"Resources": [
{
"attr1": "value1",
"attr2": "value2",
"attr3": "value3",
"attr4": "value4",
},
{
"attr1": "value5",
"attr2": "value6",
"attr3": "value7",
"attr4": "value8",
},
...
]
}
为什么会出现这个错误?
我应该如何合并它们?
编辑:
正确的答案是 tonakai 的答案。不幸的是,它是一个评论,我不能将其设置为采纳的答案。
英文:
All the answers I found for the same question involve org.json.JSONArray
. I need a solution for javax.json.JsonArray.
I have two JSON arrays and when I try to merge them with array1.addAll(array2)
I get an java.lang.UnsupportedOperationException
.
...
JsonObject users = getUsers(searchBase, filter, (String[]) null);
...
final JsonObject guests = getUsers(searchBase, filter, (String[]) null);
users.getJsonArray("Resources").addAll(guests.getJsonArray("Resources"));
...
The JsonObjects
(users and guests) I get back have both this form:
{
"startIndex": 1,
"totalResults": 7,
"itemsPerPage": 7,
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
"Resources": [
{
"attr1": "value1",
"attr2": "value2",
"attr3": "value3",
"attr4": "value4",
},
{
"attr1": "value5",
"attr2": "value6",
"attr3": "value7",
"attr4": "value8",
},
...
]
}
Why do I get this error?
How do I have to do to merge them then?
EDIT:
The correct answer is the one of tonakai. Unfortunately it is a comment and I cannot set it as the accepted answer.
答案1
得分: 2
这是您要求的代码的翻译部分:
我猜你在寻找 `JsonArrayBuilder`。
以下代码对我起作用:
import java.io.*;
import javax.json.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException {
InputStream fileInputStream = new FileInputStream("绝对文件路径\\jsonData.json");
JsonReader jsonReader = Json.createReader(fileInputStream);
JsonObject jsonObject = jsonReader.readObject();
JsonArrayBuilder users = Json.createArrayBuilder(jsonObject.getJsonArray("Resources"));
JsonArrayBuilder guests = Json.createArrayBuilder(jsonObject.getJsonArray("Resources"));
users.addAll(guests);
System.out.println(users.build());
jsonReader.close();
fileInputStream.close();
}
}
我在这里使用的文件是基于您在问题中提供的 JSON 创建的:
{
"startIndex": 1,
"totalResults": 7,
"itemsPerPage": 7,
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
"Resources": [
{
"attr1": "value1",
"attr2": "value2",
"attr3": "value3",
"attr4": "value4"
},
{
"attr1": "value5",
"attr2": "value6",
"attr3": "value7",
"attr4": "value8"
}
]
}
英文:
I guess you are looking for JsonArrayBuilder
.
Following code worked for me:
import java.io.*;
import javax.json.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException {
InputStream fileInputStream = new FileInputStream("ABSOLUTE_PATH_TO_FILE\\jsonData.json");
JsonReader jsonReader = Json.createReader(fileInputStream);
JsonObject jsonObject = jsonReader.readObject();
JsonArrayBuilder users = Json.createArrayBuilder(jsonObject.getJsonArray("Resources"));
JsonArrayBuilder guests = Json.createArrayBuilder(jsonObject.getJsonArray("Resources"));
users.addAll(guests);
System.out.println(users.build());
jsonReader.close();
fileInputStream.close();
}
}
The file that I used here was created based on the JSON you provided in the question:
{
"startIndex":1,
"totalResults":7,
"itemsPerPage":7,
"schemas":[
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
],
"Resources":[
{
"attr1":"value1",
"attr2":"value2",
"attr3":"value3",
"attr4":"value4"
},
{
"attr1":"value5",
"attr2":"value6",
"attr3":"value7",
"attr4":"value8"
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论