简体   繁体   中英

I have a JSONArray of JSONObjects which are inside two more JSONObjects. How can I iterate through them and extract key information?

I have a messy JSON formatted String like so:

{
   "ResultSet":{
      "Query":"yahoo",
      "Result":[
         {
            "symbol":"YHOO",
            "name":"Yahoo! Inc.",
            "exch":"NMS",
            "type":"S",
            "exchDisp":"NASDAQ",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YAHOY",
            "name":"Yahoo Japan Corporation",
            "exch":"PNK",
            "type":"S",
            "exchDisp":"OTC Markets",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YOJ.BE",
            "name":"YAHOO JAPAN",
            "exch":"BER",
            "type":"S",
            "exchDisp":"Berlin",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YHOO.BA",
            "name":"Yahoo! Inc.",
            "exch":"BUE",
            "type":"S",
            "exchDisp":"Buenos Aires",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YOJ.SG",
            "name":"YAHOO JAPAN",
            "exch":"STU",
            "type":"S",
            "exchDisp":"Stuttgart",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YHO.HM",
            "name":"YAHOO",
            "exch":"HAM",
            "type":"S",
            "exchDisp":"Hamburg",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YHO.DU",
            "name":"YAHOO",
            "exch":"DUS",
            "type":"S",
            "exchDisp":"Dusseldorf Stock Exchange ",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YHOF.EX",
            "name":"YAHOO",
            "exch":"EUX",
            "type":"S",
            "exchDisp":"EUREX Futures and Options Exchange ",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YHO.BE",
            "name":"YAHOO",
            "exch":"BER",
            "type":"S",
            "exchDisp":"Berlin",
            "typeDisp":"Equity"
         },
         {
            "symbol":"YA-U.TI",
            "name":"YAHOO",
            "exch":"TLO",
            "type":"S",
            "exchDisp":"TLX Exchange ",
            "typeDisp":"Equity"
         }
      ]
   }
}

Basically I need to create a String Array that goes through and gets all the values named "symbol". So for this instance I would have:

array[0] = YHOO
array[1] = YAHOY
array[2] = YOJ.BE
...
array[9] = YA-U.TI

I'm using Android, and I am trying to use the JSONObject and JSONArray classes to achieve this, but I can't seem to find a way to iterate through the inner array of objects. Does anyone here have any idea how to do this? Thanks in advance

Try an iteration through all members.

ArrayList<String> list = new ArrayList<String>();
    try {
        JSONObject jObject = new JSONObject(myJSONStr);
        JSONArray jArray = jObject.getJSONObject("ResultSet").getJSONArray("Result");
        for(int i=0; i<jArray.length(); i++){
            list.add(jArray.getJSONObject(i).getString("symbol"));
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    //In this point 'list' contains all the info you need.
            //If you need it as an array of strings, try the following
    String[] listArray = (String[]) list.toArray();

Hope it helps.

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