我想从我从API收到的JSON数据中提取数据 我知道如何从Jsonarray中检索简单的jsonArray数据或JsonObject,但我是JSON嵌套的新手。
如何提取此数据?
{
"call_log": {
"7837369400": {
"7000011216180827872": {
"start_date": "01 Dec 2014",
"start_time": "06:08 PM",
"end_date": "01 Dec 2014",
"end_time": "06:10 PM",
"call_sdate": "2014-12-01 18:08:27.000",
"call_edate": "2014-12-01 18:10:03.000",
"call_type": "1",
"caller": "0000000000",
"duartion": "94",
"call_duartion": "01:34",
"dtmf": "NA",
"dt_number": "0000000000",
"recording_path": "28.wav",
"agent_mobile": "0000000000",
"agent_name": "something"
},
"7000301116163015079": {
"start_date": "30 Nov 2014",
"start_time": "04:30 PM",
"end_date": "30 Nov 2014",
"end_time": "04:31 PM",
"call_sdate": "2014-11-30 16:30:15.000",
"call_edate": "2014-11-30 16:31:14.000",
"call_type": "1",
"caller": "0000000000",
"duartion": "59",
"call_duartion": "00:59",
"dtmf": "NA",
"dt_number": "0000000000",
"recording_path": "30.wav",
"agent_mobile": "0000000000",
"agent_name": "something"
}
}
}
}
我正在尝试的代码是:
try {
JSONObject jObject= new JSONObject(response).getJSONObject("call_log");
Iterator<String> keys = jObject.keys();
while( keys.hasNext() )
{
String key = keys.next();
// __________________________
// __________________________
Log.v("**********", "**********");
Log.v("category key", key);
// JSONObject jO= new JSONObject(response).getJSONObject(key);
// Log.v("next is", jO.toString());
// JSONObject innerJObject = jObject.getJSONObject(key);
// String name = innerJObject.getString("start_date");
// String term_id = innerJObject.getString("start_time");
//Log.v("name = "+name, "term_id = "+term_id);
}
}
catch (JSONException e){
e.printStackTrace();
}
答案 0 :(得分:1)
如果我正确地阅读了JSON ......
1)获取通话记录
2)获取数字
3)获取该号码的条目
4)从该条目中获取值
JSONObject responseObject = new JSONObject(response);
JSONObject callLog = responseObject.getJSONObject("call_log"); // 1
Iterator<String> phoneNumbers = callLog.keys(); // 2
while( phoneNumbers.hasNext() ) {
JSONObject numberLog = callLog.getJSONObject(phoneNumbers.next());
Iterator<String> callEntries = numberLog.keys(); // 3
while( callEntries.hasNext() ) {
JSONObject entry = numberLog.getJSONObject(callEntries.next());
// 4
String name = entry.getString("start_date");