繁体   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