2017-02-22 105 views
0

我有兩個JSON對象如何合併兩個JSON對象?

JsonObject taJson = Json.createObjectBuilder() 
      .add(JSON_NAME, ta.getName()).build(); 
JsonObject taJsontwo = Json.createObjectBuilder() 
      .add(JSON_EMAIL, ta.getEmail()).build(); 
array.add(taJson); 
array.add(taJsontwo); 

這就造成了兩個獨立的JSON對象,一個名字,一個用於電子郵件。我只需要一個名稱和電子郵件的對象。我不是很精通JSON或JavaFX的,所以我試圖像

JsonObject taJson = Json.createObjectBuilder() 
       .add(JSON_NAME, ta.getName()).build(); 
taJson.add(JSON_EMAIL, ta.getEmail()).build(); 

JsonObject taJson = Json.createObjectBuilder() 
       .add(JSON_NAME, ta.getName()).build(); 
JsonObject taJson = Json.createObjectBuilder() 
       .add(JSON_EMAIL, ta.getEmail()).build(); 

但這些都不能工作的事情。

+0

是你的問題,你有兩個'JsonObject'作爲輸入,或者你想創建一個有兩個屬性,你只是不知該如何? – Alexander

回答

0

嘗試以下操作:

JsonObject taJson = Json.createObjectBuilder() 
      .add(JSON_NAME, ta.getName()).build(); 
JsonObject taJsontwo = Json.createObjectBuilder() 
      .add(JSON_EMAIL, ta.getEmail()).build(); 
JSONObject combined = new JSONObject(); 
combined.put("name", taJson); 
combined.put("email", taJsontwo); 
0

而不是創建兩個對象,將其添加到像下面的代碼生成器:

JsonObjectBuilder builder = Json.createObjectBuilder().add(JSON_NAME, ta.getName()); 
builder.add(JSON_EMAIL, ta.getEmail()); 

而且做建築的時候,創建對象如下:

JsonObect taJson = builder.build();