简体   繁体   中英

Android Studio extracting data from json object not working.

Given the below successfully returned json object, how to extract the two data fields I've noted:

{
  "Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "^DJI",
    "3. Last Refreshed": "2017-06-08",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
  },
  "Time Series (Daily)": {
    "2017-06-08": {
      "1. open": "21169.7598",   <--------- I want this one.
      "2. high": "21265.6895",
      "3. low": "21138.1602",
      "4. close": "21182.5293",  <--------- and this one.
      "5. volume": "323461038"
    },
    "2017-06-07": {
      "1. open": "21171.5703",
      "2. high": "21189.8398",
      "3. low": "21113.3105",
      "4. close": "21173.6895",
      "5. volume": "273400000"
    }, etc, etc...

I've tried various methods, the below seems to be the proper one, but it raises a json exception and data[0] and data[1] log nothing.

String[] data = new String[3];
try {
    //Log.d("res2 = ", responseBody);
    JSONObject json_object = new JSONObject(responseBody);
    //json_object = json_object.getJSONObject("Meta Data");
    List<Map<String, String>> tickerData = new ArrayList<Map<String, String>>();
    HashMap<String, String> m = new HashMap<String, String>();
    m.put("1. open", json_object.getJSONObject("1. open").toString());
    m.put("4. close", json_object.getJSONObject("4. close").toString());
    tickerData.add(m);
    data[0] = tickerData.get(0).get("1. open");
    data[1] = tickerData.get(1).get("4. close");
    Log.d("data[0]=", data[0]);
    Log.d("data[1]", data[1]);
    }
catch (JSONException e) {
   Log.d("err ", "json exception at getIndexData()");
}

You can parse like this,

public class FeedParser {
    public static void parse(String response) {
        try {
            JSONObject baseObject = new JSONObject(response);

            if (baseObject == null) {
                return;
            }

            // You need Time Series (Daily) so we are going directly there.
            // Time Series (Daily) is inside baseObject
            JSONObject timeSeriesObj = baseObject.optJSONObject("Time Series (Daily)");

            if (timeSeriesObj == null) {
                return;
            }

            // We have list of dates inside Time Series (Daily) object we can iterate it using keys
            Iterator<String> iterator = timeSeriesObj.keys();

            List<Map<String, String>> tickerData = new ArrayList<Map<String, String>>();

            while (iterator.hasNext()) {
                String key = iterator.next();

                if (key != null) {
                    HashMap<String, String> m = new HashMap<String, String>();

                    JSONObject finalObj = timeSeriesObj.optJSONObject(key);

                    m.put("1. open", finalObj.optString("1. open"));
                    m.put("2. high", finalObj.optString("2. high"));
                    m.put("3. low", finalObj.optString("3. low"));
                    m.put("4. close", finalObj.optString("4. close"));
                    m.put("5. volume", finalObj.optString("5. volume"));

                    tickerData.add(m);
                }
            }

            // Below commented code you can use if your want to store the date as well
//            Map<String, Metric> values = new HashMap<>();
//
//            while (iterator.hasNext()) {
//                String key = iterator.next();
//
//                if (key != null) {
//                    HashMap<String, String> m = new HashMap<String, String>();
//                    Metric metric = new Metric();
//
//                    JSONObject finalObj = timeSeriesObj.optJSONObject(key);
//
//                    metric.open = finalObj.optString("1. open");
//                    metric.high = finalObj.optString("2. high");
//                    metric.low = finalObj.optString("3. low");
//                    metric.close = finalObj.optString("4. close");
//                    metric.volume = finalObj.optString("5. volume");
//
//                    values.put(key, metric);
//                }
//            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

This below code is optional if you use the commented code

class Metric {
    String open;
    String high;
    String low;
    String close;
    String volume;
}

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