Android Studio从json对象中提取数据不起作用。

时间:2017-06-08 21:34:45

标签: java android json hashmap

鉴于以下成功返回的json对象,如何提取我注意到的两个数据字段:

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

我尝试了各种方法,下面似乎是正确的方法,但它引发了一个json异常,数据[0]和数据[1]没有记录任何内容。

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()");
}

1 个答案:

答案 0 :(得分:2)

您可以这样解析,

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();
        }
    }
}

如果您使用注释代码

,以下代码是可选的
class Metric {
    String open;
    String high;
    String low;
    String close;
    String volume;
}