[英]Extract my JSON Result in Android
我的JSon數組已經返回了
[
{"StudentID":"BS231",
"ChildName":"Vishesh Malhotra",
"ClassName":"4th Class",
"Attendance":false},
{"StudentID":"BS233",
"ChildName":"Anisha Malhotra",
"ClassName":"6th Class",
"Attendance":false
}
]
我想提取它,我知道我可以將它放入JSONObject進行初始化,但是現在使用optJSONArray函數我沒有要提取的數組名稱
JSONObject jsonResponse = new JSONObject(result);
JSONArray data = jsonResponse.optJSONArray("ArrayNameWhichIsMissing");
請告訴我如何提取該數組。
[ // 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
}
]
你有一個json數組
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
}
記錄輸出
....... BS231
....... BS233
我認為你可以得到回應,因為JSONArray也是這樣的
JSONArray objj = new JSONArray("your json string");
評論我的結果
解析Json的簡單方法是使用GSON lib轉換josn對象或數組以轉換為java類對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.