2017-01-09 4 views
0

以下のすべてのオブジェクトを1つのオブジェクトとして再帰的にマージしたいと思います。文字列オブジェクトを受け取るたびに、繰り返し実行するたびにコードを実行するたびに、それらをリストに格納しています。リストは以下のようになります。以下の値を1つの値としてリストにマージする

bean [String1、String2、String3]。これら3つの文字列は、単一の文字列オブジェクトとしてマージされます。

文字列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 
     } 
] 

は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構造のようです。

あなたがしなければならないことは、すべての応答の値( '更新された'フィールドの値)を単一のJsonArrayに追加するグローバル配列です。

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

あなたの要件のニーズと必要があるとして、今、あなたは任意のオブジェクトの内部でこのJsonArrayを使用することができますが、次のようにGsonライブラリを使用して

は、あなたはそれを行うことができます。

関連する問題