【问题标题】:merge the below values in a list as a single value将列表中的以下值合并为单个值
【发布时间】:2017-01-10 20:53:45
【问题描述】:

我想递归地将以下所有对象合并为一个对象。每次我为每次迭代运行代码时,我都会收到一个字符串对象,我将它们存储在一个列表中。列表如下所示:

bean [String1,String2,String3]。这三个字符串将被合并为一个字符串对象。

字符串1:

 [code=100,
    response=
        {
          "testObjects": [
            {
              "updated": [
        {
          "attributes": {},
          "id": "123"
        },
        {
          "attributes": {},
          "id": "456"
        }
      ],
              "timeCheckQuery": null,
              "query": null,
              "message": null,
              "error": null,
              "deleted": null,
              "agentId": null,
              "added": null
            }
          ],
          "message": null,
          "error": null
        }
    ]

字符串2:

[code=100,
    response=
    {
          "testObjects": [
            {
              "updated": [
    {
      "attributes": {},
      "id": "789"
    },
    {
      "attributes": {},
      "id": "759"
    }
  ],
              "timeCheckQuery": null,
              "query": null,
              "message": null,
              "error": null,
              "deleted": null,
              "agentId": null,
              "added": null
            }
          ],
          "message": null,
          "error": null
        }
]

字符串3:

[code=100,
    response=
    {
          "testObjects": [
            {
              "updated": [
    {
      "attributes": {},
      "id": "242"
    },
    {
      "attributes": {},
      "id": "951"
    }
  ],
              "timeCheckQuery": null,
              "query": null,
              "message": null,
              "error": null,
              "deleted": null,
              "agentId": null,
              "added": null
            }
          ],
          "message": null,
          "error": null
        }
]

输出:

[code=300,
        response=
        {
              "testObjects": [
                {
                  "updated": [
        {
          "attributes": {},
          "id": "123"
        },
        {
          "attributes": {},
          "id": "456"
        },
{
          "attributes": {},
          "id": "789"
        },
        {
          "attributes": {},
          "id": "759"
        },
 {
          "attributes": {},
          "id": "242"
        },
        {
          "attributes": {},
          "id": "951"
        }
      ],
                  "timeCheckQuery": null,
                  "query": null,
                  "message": null,
                  "error": null,
                  "deleted": null,
                  "agentId": null,
                  "added": null
                }
              ],
              "message": null,
              "error": null
            }
    ]

【问题讨论】:

标签: java json


【解决方案1】:

您可以将第一个对象序列化为 json 字符串,然后将该字符串附加到序列化的下一个对象,依此类推。

【讨论】:

  • 这应该是评论,而不是答案。
【解决方案2】:

“更新”字段的值似乎是一个 JsonArray 结构。

您需要做的是有一个全局数组,它将所有响应中的值(“更新”字段的值)添加到单个 JsonArray 中。

使用 Gson 库,您可以按如下方式进行操作

JsonArray jsonarray = new JsonArray();
jsonarray.addAll(jsonObject1.get("updated").getAsJsonArray());
jsonarray.addAll(jsonObject2.get("updated").getAsJsonArray());
jsonarray.addAll(jsonObject2.get("updated").getAsJsonArray());

现在您可以根据需要在任何对象中使用此 JsonArray。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-08-31
    • 2019-07-09
    相关资源
    最近更新 更多