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 :)
New to Java, wanted some help with reading .json object arrays.
using JSONArray, JSONObject etc. to parse JSON with Java
Videos
// 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?
Hi,
I am reading json into a Java program, where I am attempting to process it. I'm having some challenges figuring out how to dig down into the data structure, in order to iterate through some of the deeper lists. I'm attaching below excerpts of the Java, and the toy/test json.
Here is the toy/test json:
{
"objects": [
{
"type": "object-1",
"coordinates": [
[
0,
5
],
[
5,
10
],
[
10,
15
],
[
15,
0
]
]
}
]
}What I WANT to do is grab the list of objects associated with the key "objects", and then iterate through the dictionaries that compose the list, further extracting components (coordinates, say, if the type is what i'm after).
The thing is, I don't know how to do this. The code below is an attempt, but it doesn't work.
Here's the Java excerpt:
JSONObject json;
void setup() {
size(800, 494);
// input json file
String inputJsonFile = "input.json";
String inputJsonFileContentsString = "";
try {
inputJsonFileContentsString = readFileIntoString( inputJsonFile );
}
catch (IOException e) {
e.printStackTrace();
}
//JSON parser object to parse read file
JSONObject inputJsonData = parseJSONObject( inputJsonFileContentsString );
// recover list of objects
JSONArray objects = inputJsonData.getJSONArray( "objects" );
// print json data: iterate over list of objects, retrieving type and coordinates
int arrayLength = objects.size();
for ( int i = 0; i < arrayLength; i++ ) {
// THIS DOESN'T WORK
// I want the next dictionary/hash, but how to retrieve it?
JSONObject objectDataHash = objects[ i ];
}
}Thank you. I normally work with Python, it's been a while since I've done a lot of coding in Java so I'm probably missing something obvious.