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

标签 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 异常,并且 data[0] 和 data[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()");
}

最佳答案

你可以这样解析,

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

关于java - Android Studio 从 json 对象提取数据不起作用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445951/

相关文章:

java - 寻找字谜的简单算法

java - 为什么我的异常类需要序列化?

android - Xamarin.Forms WebChromeClient

ios - 如何使用 Json (Swift 2) 将错误处理添加到工作 TableView Controller

java - RMI 何时建立 TCP 连接?

java - Spring WebSocket集成测试 Artifact 哪里找?

android - 统一: JDK stop working

android - Android M 上配置文件 GPU 条的颜色

JSON 架构 : Why does "constant" not validate the same way as a single-valued "enum"?

c++ - 为什么 Qt 拒绝有效的 JSON?