在另一个JsonObject中提取JsonObject
问题描述:
我想从我从API接收的JSON数据中提取数据。
我知道如何从Jsonarray中检索简单的jsonArray数据或JsonObject,但我是JSON嵌套的新手。在另一个JsonObject中提取JsonObject
如何提取此数据?
{
"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();
}
答
如果我读了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");
okk现在我已经纠正了我的问题,我用ur代码来获取数据,但得到错误在行JSONObject entryObject = numberLog.get(entry);如其所说必需的org.json.JSONObject,但发现java.lang.Object ... @ cricket_007 –
糟糕。 'getJSONObject' –
非常感谢你@ cricket_007 –