如何使用Gson将JSON字符串列表转换为JsonObject。

huangapple go评论70阅读模式
英文:

How to convert list of Json string to JsonObject using Gson

问题

我有一个 JSON 字符串列表:List<String> jsonStringList,我想使用 Gson 将它们转换为单个 JsonObject,我想知道最佳方法是什么?

我知道我可以逐个解析它们,就像这样:

for (String jsonString : jsonStringList) {
    JsonElement parsed = new JsonParser().parse(jsonString);
}

但是我该如何将它们合并在一起?最佳方法是什么?

英文:

I have a list of Json string: List&lt;String&gt; jsonStringList, I want to convert it to a single JsonObject using Gson, may I know what is the best way to do it?

I know I can parse them one by one like:

for (String jsonString : jsonStringList) {
    JsonElement parsed = new JsonParser().parse(jsonString);
}

But how do I join them together? What is the best way to do it?

答案1

得分: 1

String s1 = "{\n" +
            "    \"userName\": \"RandomDiscord Name\",\n" +
            "    \"permissions\": \"read\"\n" +
            "  }";
String s2 = "{\n" +
            "    \"userName\": \"RandomDiscord Name1\",\n" +
            "    \"permissions\": \"read2\"\n" +
            "  }";
List<String> jsonStringList = Arrays.asList(new String[]{s1, s2});
JSONArray arr = new JSONArray();
for (String jsonString : jsonStringList) {
    JsonElement obj = new JsonParser().parse(jsonString);
    arr.add(obj);
}
String jsonString = arr.toString();
System.out.println(jsonString);

JSONObject obj = new JSONObject();
try {
    obj.put("CustomObject", arr);
} catch(JSONParseException e) {
    e.printStackTrace();
}
System.out.println(obj.toString());

输出结果:

[
    {"userName":"RandomDiscord Name","permissions":"read"},
    {"userName":"RandomDiscord Name1","permissions":"read2"}
]
{"CustomObject":[{"userName":"RandomDiscord Name","permissions":"read"},{"userName":"RandomDiscord Name1","permissions":"read2"}]}
英文:
        String s1 = &quot;{\n&quot; +
                &quot;    \&quot;userName\&quot;: \&quot;RandomDiscord Name\&quot;,\n&quot; +
                &quot;    \&quot;permissions\&quot;: \&quot;read\&quot;\n&quot; +
                &quot;  }&quot;;
        String s2 = &quot;{\n&quot; +
                &quot;    \&quot;userName\&quot;: \&quot;RandomDiscord Name1\&quot;,\n&quot; +
                &quot;    \&quot;permissions\&quot;: \&quot;read2\&quot;\n&quot; +
                &quot;  }&quot;;
        List&lt;String&gt; jsonStringList = Arrays.asList(new String[]{s1, s2});
        JSONArray arr = new JSONArray();
        for (String jsonString : jsonStringList) {
            JsonElement obj = new JsonParser().parse(jsonString);
            arr.add(obj);
        }
        String jsonString = arr.toString();
        System.out.println(jsonString);

        JSONObject obj = new JSONObject();
        try {
            obj.put(&quot;CustomObject&quot;, arr);
        } catch(JSONParseException e) {
            e.printStackTrace();
        }
        System.out.println(obj.toString());

Try to use JSONArray and while iterating List<String> just add that object into JsonArray.

For above code, i am getting below output
O/P

[{&quot;userName&quot;:&quot;RandomDiscord Name&quot;,&quot;permissions&quot;:&quot;read&quot;},{&quot;userName&quot;:&quot;RandomDiscord Name1&quot;,&quot;permissions&quot;:&quot;read2&quot;}]
{&quot;CustomObject&quot;:[{&quot;userName&quot;:&quot;RandomDiscord Name&quot;,&quot;permissions&quot;:&quot;read&quot;},{&quot;userName&quot;:&quot;RandomDiscord Name1&quot;,&quot;permissions&quot;:&quot;read2&quot;}]}

huangapple
  • 本文由 发表于 2020年8月20日 18:17:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定