2017-01-09 71 views
0

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

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

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

输出:

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

可能的重复http://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-string – Kagemusha

回答

0

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

+0

这应该是一个评论,而不是一个答案。 –

0

'updated'字段的值似乎是JsonArray结构。

你需要做的是有一个全局数组,它将所有响应的值('updated'字段的值)添加到一个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任何物体内做到这一点。