0

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?

4
  • 1
    jsonArray.add(jsonObject); Commented Feb 23, 2017 at 14:03
  • which json api are you using ? jackson or gson or any ? Commented Feb 23, 2017 at 14:04
  • learn more about json api in java docs.oracle.com/javaee/7/api/javax/json/package-summary.html Commented Feb 23, 2017 at 14:04
  • @Darshit org.json.simple Commented Feb 23, 2017 at 14:20

1 Answer 1

0

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

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

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"
        ]
    }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.