Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
Answer from notnoop on Stack Overflowjson - Accessing members of items in a JSONArray with Java - Stack Overflow
New to Java, wanted some help with reading .json object arrays.
using JSONArray, JSONObject etc. to parse json with Java in Processing
using JSONArray, JSONObject etc. to parse JSON with Java
Videos
Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
An org.json.JSONArray is not iterable.
Here's how I process elements in a net.sf.json.JSONArray:
JSONArray lineItems = jsonObject.getJSONArray("lineItems");
for (Object o : lineItems) {
JSONObject jsonLineItem = (JSONObject) o;
String key = jsonLineItem.getString("key");
String value = jsonLineItem.getString("value");
...
}
Works great... :)
// 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 in Processing. 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'm 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. Thank you.