简体   繁体   中英

How to get Json values using its key in java?

I have JSON array like given below. How to get the values based on its keys?

$> 

[{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"

days 0 Hrs 0 Mins ","lsDwn":"2012-06-21"}]}]

Use following code to parse your json String :

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...
}

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