簡體   English   中英

如何合並兩個JSON對象?

[英]How to merge two JSON objects?

我有兩個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();

但是這些都不起作用。

請嘗試以下操作:

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);

與其創建兩個對象,不如將其添加到構建器中,如以下代碼所示:

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

構建完成后,如下所示創建對象:

JsonObect taJson = builder.build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM