Here is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
Answer from Grammin on Stack OverflowHere is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.
String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);
Hope this helps :)
How to turn json objects into an array of json objects using Java?
New to Java, wanted some help with reading .json object arrays.
How to parse Json array with 2 or more different types using Jackson?
http://www.baeldung.com/jackson-inheritance
You'll need to have those types inherit from one base class though.
Also; that is really bad JSON.
More on reddit.comJackson parsing JSON containing an array of objects and array of maps w/ dynamic keys
Videos
Hi. I'm new to java and stuck wondering how I would go about turning something like this: {} {} {} into this: [{},{},{}]. I'm trying to figure out how to do this to my incoming json data using Java. The data has like 20 similar objects, with the same keys but with constantly changing values.
Any help is much appreciated.
Sincerely,
your friendly neighborhood noob
// Create a JSON object from the response
JSONObject jsonResponse = new JSONObject(response.toString());
// Write the JSON object to a file
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.json"))) {
writer.write(jsonResponse.toString(4)); // The number specifies the indentation for pretty printing
}
System.out.println("JSON response written to output.json");
} else {
System.out.println("HTTP request failed with response code: " + responseCode);
}
// Read the JSON data from the file
String jsonContent = new String(Files.readAllBytes(Paths.get("output.json")));
// Create a JSONObject from the JSON data
JSONObject jsonObject = new JSONObject(jsonContent);
JSONArray result = jsonObject.getJSONArray("result");
JSONObject skey = result.getJSONObject(5);
String memo = skey.getString("memo");
System.out.println("Value of 'key' in the JSON object: " + memo);
// Close the connection
connection.disconnect();Code ^^
i am trying to read a specific sub-key of a .json array which is saved from an http GET request.
the structure of the .json file is something like this:
"jsonarray": [{
"subkey1": "a"
"subkey2": "b"
"subkey3": "c"
"subkey4": "d"
"subkey5": "e"
"subkey6": "f"
"subkey7": "g"
}],
"key1": "h",
"key2": "i"i want to read the value of subkey5 i.e. "e", but when i try to do it, it gives me an error. where am i going wrong?