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 OverflowHave 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... :)
Here is the idea :
JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sports");
// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);
// and details of the first element
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");
// and so on, you can process leaguesArray similarly
It should work (feel free to complain about compile errors if there are any)
Your JSON data is an object (it starts with a curly brace). In the next inner layer, there is a single array (at key "sports"):
String jsonString = readURL("//my URL is here");
JSONObject result = JSONObject(jsonString);
JSONArray sports = result.getJSONArray("sports");
JSONObject sport = sport.getJSONObject(0);
System.out.println(sport.getString("name"));
I might have used another JSON library than you.
How do you get a specific value in json text in java?
New to Java, wanted some help with reading .json object arrays.
java - How to Get JSON Array Within JSON Object? - Stack Overflow
[Java] How can I parse a JSON string then extract values from it?
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?
Solved, use array list of string to get name from Ingredients. Use below code:
JSONObject jsonObj = new JSONObject(jsonStr);
//extracting data array from json string
JSONArray ja_data = jsonObj.getJSONArray("data");
int length = ja_data.length();
//loop to get all json objects from data json array
for(int i=0; i<length; i++){
JSONObject jObj = ja_data.getJSONObject(i);
Toast.makeText(this, jObj.getString("Name"), Toast.LENGTH_LONG).show();
// getting inner array Ingredients
JSONArray ja = jObj.getJSONArray("Ingredients");
int len = ja.length();
ArrayList<String> Ingredients_names = new ArrayList<>();
for(int j=0; j<len; j++){
JSONObject json = ja.getJSONObject(j);
Ingredients_names.add(json.getString("name"));
}
}
JSONObject jsonObject =new JSONObject(jsonStr);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length;i++){
JSONObject json = jsonArray.getJSONObject(i);
String id = json.getString("id");
String name=json.getString("name");
JSONArray ingArray = json.getJSONArray("Ingredients") // here you are going to get ingredients
for(int j=0;j<ingArray.length;j++){
JSONObject ingredObject= ingArray.getJSONObject(j);
String ingName = ingredObject.getString("name");//so you are going to get ingredient name
Log.e("name",ingName); // you will get
}
}