2017-01-09 42 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任何物體內做到這一點。

相關問題