Reputation: 5691
I have JSON code like above, as a response:
"candidates": [
{
"subtest1": "0.802138030529022",
"enrollment_timestamp": "1416850761"
},
{
"elizabeth": "0.802138030529022",
"enrollment_timestamp": "1417207485"
},
{
"elizabeth": "0.777253568172455",
"enrollment_timestamp": "1416518415"
},
{
"elizabeth": "0.777253568172455",
"enrollment_timestamp": "1416431816"
}
]
I try to get names from candidates
array.
public void dataCheck(String text){
System.out.println("JSON response:");
System.out.println(text);
try {
JSONObject jsonRootObject = new JSONObject(text);
JSONArray jsonArray = jsonRootObject.optJSONArray("candidates");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String subtest1 = jsonObject.optString("subtest1").toString();
}
} catch (JSONException e){
e.printStackTrace();
}
}
It is hard, because these values are in an array of an array and without identifier. Identifier is the exact value, so couldn't define variable in my code. I need only first value, like subtest1
in this example.
Upvotes: 0
Views: 152
Reputation: 4025
// Get the keys in the first JSON object
Iterator<?> keys = jsonObject.keys();
if (keys.hasNext()) {
// Get the key
String key = (String)keys.next();
String objValue = jsonObject.getString(key);
...
}
Upvotes: 1