简体   繁体   中英

Extract my JSON Result in Android

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.

[ // 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

I think you can get response as JSONArray also like this

JSONArray objj = new JSONArray("your json string");

comment me about result

解析Json的简单方法是使用GSON lib转换josn对象或数组以转换为java类对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM