0

I would like to merge all the below objects as a single object recursively. Every time I run the code for every iteration I receive a string object, I am storing them in a list. List looks like below:

bean [String1, String2, String3]. These three strings are to be merged as a single string object.

String1:

 [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
        }
    ]

String2:

[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
        }
]

String3:

[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
        }
]

output:

[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
            }
    ]
David Sam
  • 59
  • 9

2 Answers2

0

You could serialize the first object as a json string, then append that string with next object serialized and so on.

Shenron
  • 420
  • 4
  • 11
0

The value of the 'updated' field seems to be a JsonArray Structure.

What you have to do is have a Global Array that adds the values(value of the 'updated' field) from all the responses into a single JsonArray.

Using Gson Library you can do it as follows

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

Now you can use this JsonArray inside any object as you need as your requirement needs.

Kaushik Patel
  • 239
  • 1
  • 5