如何在java代码中使用jsonArray读取json响应

时间:2017-03-23 04:59:00

标签: arrays json httpclient

我有一个查询,我必须从下面提到的json响应中的“studentData”键下读取键“class”。 {

“studentData”:[     {       “名字”:“约翰”,       “班级”:2,       “rollno”:“2015”     }   ]   “yearofenrollment”:2017年   }

请帮忙。

提前致谢

1 个答案:

答案 0 :(得分:0)

JSONObject jsonObjectResponse = new JSONObject(response.toString()); //obtain the object
JSONArray jsonMainNode = jsonObjectResponse.optJSONArray("studentData");//get array from object
JSONObject jsonChildNode = jsonMainNode.getJSONObject(0);//get first object in array
String studentDataValue = jsonChildNode.optString("class");//obtain value from class key
相关问题