json包来处理来自API的JSON对象和数组。我的问题不一定是迭代,但是我下拉的数据的一些实例是JSON数组,然后是JSON对象。下面是我正在下载的实例的数据,我在这里谈论。如果查看第一组数据的键CustomerLog,它是一个数组,但是如果查看第二组数据,它是一个对象。
我使用for语句和嵌套的while循环来获取此信息。但我得到了一个异常抛出,因为我提到的情况是,CustomerLog有时是数组,有时是对象。
抛出异常是因为我使用getJSONArray方法请求JSON Array,但它是一个对象。我真的需要帮助,因为我真的在拖延,找不到任何解决我的问题的答案。任何帮助都是非常感谢的。
for (int k = 0; k < customer.length(); k++)
{
try
{
customerlog = customer.getJSONObject(k).get("CustomerLogs");
System.out.println(customerlog.toString());
if(customerlog instanceof org.json.JSONObject)
{
Object logs = ((org.json.JSONObject) customerlog).getJSONArray("CustomLog");
Iterator iterator = ((org.json.JSONObject) logs).keys();
while (iterator.hasNext())
{
}
System.out.println(logs.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
{
"CustomerLog":[
{
"Status":"Completed",
"LastContacted":"2015-06-01 09:18:26",
"LastContactedLocal":"2015-06-01 09:18:26",
"DateRequiredFollowUp":"0000-00-00",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"34995",
"Notes":"Tried ringing: Opening hours 10am-6.30 mon-fri - will ring back later.",
"LastContactedUTC":"2015-05-31 23:18:26"
},
{
"Status":"Completed",
"LastContacted":"2015-06-01 17:12:33",
"LastContactedLocal":"2015-06-01 17:12:33",
"DateRequiredFollowUp":"0000-00-00",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"35064",
"Notes":"Spoke to a fellow there: Veronica leaves about 3pm, said for me to ring back tomorrow before then.",
"LastContactedUTC":"2015-06-01 07:12:33"
},
{
"Status":"Completed",
"LastContacted":"2015-06-02 10:14:26",
"LastContactedLocal":"2015-06-02 10:14:26",
"DateRequiredFollowUp":"2015-06-30",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"35071",
"Notes":"Spoke to Veronica: primarily interested in our photo papers (said they have a printer cart supplier already); activated their account, emailed P/L, login for website, etc.",
"LastContactedUTC":"2015-06-02 00:14:26"
},
{
"Status":"Completed",
"LastContacted":"2015-06-12 12:09:06",
"LastContactedLocal":"2015-06-12 12:09:06",
"DateRequiredFollowUp":"0000-00-00",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"35200",
"Notes":"Veronica placed order - all good; had to ring her re: 2 x paper sample books (no longer available, cancelled B/O); said to let her know if they ever became available again.",
"LastContactedUTC":"2015-06-12 02:09:06"
},
{
"Status":"Completed",
"LastContacted":"2015-07-06 09:58:25",
"LastContactedLocal":"2015-07-06 09:58:25",
"DateRequiredFollowUp":"2015-07-13",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"35546",
"Notes":"Left voicemail mssge",
"LastContactedUTC":"2015-07-05 23:58:25"
},
{
"Status":"Completed",
"LastContacted":"2015-08-06 11:41:04",
"LastContactedLocal":"2015-08-06 11:41:04",
"DateRequiredFollowUp":"2015-08-20",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"35962",
"Notes":"Left voicemail message",
"LastContactedUTC":"2015-08-06 01:41:04"
},
{
"Status":"Completed",
"LastContacted":"2015-09-04 14:32:13",
"LastContactedLocal":"2015-09-04 14:32:13",
"DateRequiredFollowUp":"2016-01-21",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"36372",
"Notes":"Spoke to Veronica: said she'd done a big order & still ample stock (prob. only need to order 6-mthly); thanked her, said I'd keep in touch.",
"LastContactedUTC":"2015-09-04 04:32:13"
},
{
"Status":"Completed",
"LastContacted":"2017-01-13 10:44:09",
"LastContactedLocal":"2017-01-13 10:44:09",
"DateRequiredFollowUp":"2017-03-10",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"39622",
"Notes":"Spoke to Veronica: said she hadn't seen a P/L for a while; wants to stay on database; emailed P/L to her.",
"LastContactedUTC":"2017-01-13 00:44:09"
},
{
"Status":"Completed",
"LastContacted":"2017-03-21 11:57:02",
"LastContactedLocal":"2017-03-21 11:57:02",
"DateRequiredFollowUp":"2017-06-20",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"40355",
"Notes":"Spoke to Veronica; has our P/L, use us as a back-up supplier; will order as needed. ",
"LastContactedUTC":"2017-03-21 01:57:02"
},
{
"Status":"Require Recontact",
"LastContacted":"2017-06-14 10:57:07",
"LastContactedLocal":"2017-06-14 10:57:07",
"DateRequiredFollowUp":"2017-08-15",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"40950",
"Notes":"Spoke to Veronica: has our P/L; right for stock (said things have been slow); talked about paper sale at the moment.",
"LastContactedUTC":"2017-06-14 00:57:07"
},
{
"Status":"Completed",
"LastContacted":"2018-06-05 10:05:54",
"LastContactedLocal":"2018-06-05 10:05:54",
"DateRequiredFollowUp":"0000-00-00",
"Customer":[
"Veronica Hartmann"
],
"FollowUpType":"",
"AllocatedTo":"Paul Banks",
"LogID":"43496",
"Notes":"still has us on file doesnt sell much paper anymore but when she does she will call us ",
"LastContactedUTC":"2018-06-05 00:05:54"
}
]
}{
"CustomerLog":{
"Status":"Completed",
"LastContacted":"2015-05-08 14:34:21",
"LastContactedLocal":"2015-05-08 14:34:21",
"DateRequiredFollowUp":"0000-00-00",
"Customer":[
"Roger "
],
"FollowUpType":"",
"AllocatedTo":"Stephen Legge",
"LogID":"34292",
"Notes":"Number disconnected - DELETE",
"LastContactedUTC":"2015-05-08 04:34:21"
}
}
发布于 2018-10-09 09:03:51
你的意思是,如果只有一个结果,API将只响应一个对象,而不是一个只有一个元素的数组?如果是这样的话,API就有一个设计问题。应该清楚何时返回单个实例,以及何时返回集合。
最快的破解方法是处理catch块中的对象:
catch (Exception e)
{
Object log = ((org.json.JSONObject) customerlog)get("CustomerLog");
// do somthing with your log here
}
发布于 2018-10-09 09:13:39
为什么不直接检查customerLog
是不是一个数组?if(Array.isArray(customerLog))
,然后再决定如何处理你的数据?
https://stackoverflow.com/questions/52711607
复制相似问题