Reputation: 2217
My JSon Array has been returned like
[
{"StudentID":"BS231",
"ChildName":"Vishesh Malhotra",
"ClassName":"4th Class",
"Attendance":false},
{"StudentID":"BS233",
"ChildName":"Anisha Malhotra",
"ClassName":"6th Class",
"Attendance":false
}
]
I want to extract it, I know i can put it into JSONObject to initialize, but while using optJSONArray function now i have no Array Name to extract
JSONObject jsonResponse = new JSONObject(result);
JSONArray data = jsonResponse.optJSONArray("ArrayNameWhichIsMissing");
kindly tell how can i extract that Array.
Upvotes: 1
Views: 354
Reputation: 133560
[ // json array node
{ // json object node
"StudentID": "BS231",
"ChildName": "Vishesh Malhotra",
"ClassName": "4th Class",
"Attendance": false
},
{
"StudentID": "BS233",
"ChildName": "Anisha Malhotra",
"ClassName": "6th Class",
"Attendance": false
}
]
You have a json array
JSONArray jr = new JSONArray("myjsonstring");
for(int i =0;i<jr.length();i++)
{
JSONObject jb = (JSONObject)jr.get(i);
String id = jb.getString("StudentID");
Log.i(".......",id);
// Similar for other ChildName and ClassName
// use getBoolean for attendance
}
Log output
....... BS231
....... BS233
Upvotes: 3
Reputation: 682
I think you can get response as JSONArray also like this
JSONArray objj = new JSONArray("your json string");
comment me about result
Upvotes: 1