简体   繁体   中英

JSON Array parsing element by element

I have a JSON array like this:

[
   {
     "stats": {
     "callQuality": 5,
     "audioRecvRemoteMute": false,
     "audioRecvLocalMute": true,
     },
     "rtpStatsList": [
     {
        "media": 1,
        "direction": 1,
        "content": true,
     }
   ],
   "timestamp": 1460208299000
   },
   {
      "stats": {
      "callQuality": 5,
      "audioRecvRemoteMute": false,
      "audioRecvLocalMute": true,
   },
   "rtpStatsList": [
   {
      "media": 1,
      "direction": 1,
      "content": true,
   }
   ],
  "timestamp": 1460208299000
 },
]

There are multiple elements (180 elements for eg) in the array. I need "media" and "direction" parameters under rtpStatsList. Any useful suggestion to parse the same using JAVA?

Here Check this out

String data = EntityUtils.toString(httpResponse2.getEntity()); JSONArray js=new JSONArray(data) for(int i=0;i<js.length();i++) {

JSONObject total = js.getJSONObject(js);{ JSONArray rtpStatsList=total.getJSONArray("rtpStatsList"); for(int j=0;j<rtpStatsList.length();j++) {

                     `JSONObject jsonObject = rtpStatsList.getJSONObject(j);` 
                      media = jsonObject.getString("media");
                      direction = jsonObject.getString("direction");                  
                      }
                      }`

Use this: http://theoryapp.com/parse-json-in-java/ You have an extensive explanation of:

  • Build a URL.
  • Read from the URL.
  • Build a JSON object for the content.
  • Retrieve the first result from an array of results.
  • Print out the information.

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