简体   繁体   English

将JSONObject添加到JSONArray java

[英]Add JSONObject to JSONArray java

我需要将一个对象添加到JSONArray (已经包含元素),所以我必须知道要添加的属性的类型(如果它是整数或字符串等)。 java吗?

As others have suggested, you can simply use the add method in the JsonArray builder. 正如其他人所建议的那样,您可以简单地在JsonArray构建器中使用add方法。

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

This is simply used to format the JSON output. 这仅用于格式化JSON输出。

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'
}

Here is the output: 这是输出:

[
    {
        "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"
        ]
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM