如何在java中使用其键获取Json值?

时间:2012-11-20 06:20:11

标签: java android

我有下面给出的JSON数组。如何根据键获取值?

$> 

[{Work
> Group=[{"id":"wNUvBiv5tQDFphP3zITN5NDEqyXJM8W2","errRsn":["Response
> time from Sweden exceeded 1500 ms."],"ap":"100.0
> %","status":"Trouble","name":"xxxx","rsptime":"1549
> ms","downs":"0","tDwn":"5 Hrs 50 Mins
> ","conf":"no","mtype":"URL","monitorid":"2c2d1ba5b57d9c1be8fcae5522b5bb9f","lsDur":"-","lsDwn":""},{"id":"wNUvBiv5tQB1kKQSZBij\/b8FU++kkoRh","ap":"100.0
> %","status":"Up","name":"xxx","rsptime":"424
> ms","downs":"0","conf":"no","mtype":"HOMEPAGE","monitorid":"58e79381158d85e8eb944ec5506db09a","lsDur":"-"},{"id":"wNUvBiv5tQD\/DIkhHWufnTRJzbcIgZk4","rspvalue":"-","downReason":"Unknown
> Host","status":"Down","name":"xxxx","tDwn":"62 days 17 Hrs 7 Mins
> ","conf":"yes","mtype":"PORT","monitorid":"6cacfbf47ad21d824006be1168efc3c6","lsDwn":"2012-09-18"},{"id":"wNUvBiv5tQCBw1RAuk0Q2P1FZPxXxMr1","downReason":"Trouble"

天0小时0分钟“,”lsDwn“:”2012-06-21“}]}]

1 个答案:

答案 0 :(得分:3)

使用以下代码解析json字符串:

JSONArray jarray = new JSONArray("Your JSON String Here");

for (int i=0; i < jarray.length(); i++)
{
  JSONObject oneObject = jArray.getJSONObject(i);
  // Pulling items from the array
   String oneObjectsid = oneObject.getString("id");
   String oneObjectsap = oneObject.getString("ap");
   String oneObjectsstatus = oneObject.getString("status");
   String oneObjectsname = oneObject.getString("name");
   //your rest code here...
}