将JSONObject添加到JSONArray java [英] Add JSONObject to JSONArray java

查看:829
本文介绍了将JSONObject添加到JSONArray java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将对象添加到JSONArray(其中已经包含元素),因此我必须知道要添加的属性的类型(如果它是整数或字符串等). 如何使用Java将对象添加到我的数组中?

I need to add an object to a JSONArray(which contains elements already), so I have to know the type of the attribute to add, if this is an integer or a string etc,.. How to add that object to my array with java?

推荐答案

正如其他人所建议的那样,您只需在JsonArray构建器中使用add方法即可.

As others have suggested, you can simply use the add method in the JsonArray builder.

import javax.json.*;

public class JsonExample {
    public static void main(String[] args) {
        JsonObject personObject = Json.createObjectBuilder()
                .add("name", Json.createObjectBuilder()
                        .add("given", "John")
                        .add("middle", "Edward")
                        .add("surname", "Doe")
                        .build())
                .add("age", 42)
                .add("isMarried", true)
                .add("address", Json.createObjectBuilder()
                        .add("street", "Main Street")
                        .add("city", "New York")
                        .add("zipCode", "10044")
                        .add("country", "United States")
                        .build())
                .add("phoneNumber", Json.createArrayBuilder()
                        .add("+1 (718) 111-1111")
                        .add("+1 (718) 111-1112")
                        .build())
                .build();

        JsonArray personArray = Json.createArrayBuilder()
                .add(personObject) // Add person to array.
                .build();

        System.out.println(JsonUtil.prettyPrint(personArray));
    }
}

这仅用于格式化JSON输出.

This is simply used to format the JSON output.

import java.io.StringWriter;
import java.util.*;
import javax.json.*;
import javax.json.stream.JsonGenerator;

/* Based on: http://stackoverflow.com/a/32500882/1762224 */
public class JsonUtil {
    public static String prettyPrint(JsonStructure json) {
        return jsonFormat(json, JsonGenerator.PRETTY_PRINTING);
    }

    public static String jsonFormat(JsonStructure json, String... options) {
        StringWriter stringWriter = new StringWriter();
        Map<String, Boolean> config = buildConfig(options);
        JsonWriterFactory writerFactory = Json.createWriterFactory(config);
        JsonWriter jsonWriter = writerFactory.createWriter(stringWriter);

        jsonWriter.write(json);
        jsonWriter.close();

        return stringWriter.toString();
    }

    private static Map<String, Boolean> buildConfig(String... options) {
        Map<String, Boolean> config = new HashMap<String, Boolean>();

        if (options != null) {
            for (String option : options) {
                config.put(option, true);
            }
        }

        return config;
    }
}

此示例使用以下依赖项:

This example uses the following dependencies:

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    compile 'javax.json:javax.json-api:1.1.0-M1'
    compile 'org.glassfish:javax.json:1.1.0-M1'
}

以下是输出:

[
    {
        "name": {
            "given": "John",
            "middle": "Edward",
            "surname": "Doe"
        },
        "age": 42,
        "isMarried": true,
        "address": {
            "street": "Main Street",
            "city": "New York",
            "zipCode": "10044",
            "country": "United States"
        },
        "phoneNumber": [
            "+1 (718) 111-1111",
            "+1 (718) 111-1112"
        ]
    }
]

这篇关于将JSONObject添加到JSONArray java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆