【发布时间】:2017-06-08 21:34:45
【问题描述】:
鉴于以下成功返回的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()");
}
【问题讨论】: