пакет json для обработки моих JSON-объектов и массивов из API.Моя проблема не обязательно повторяется, однако некоторые экземпляры данных, которые я собираю, это JSON Array, а затем объекты JSON.Ниже приводятся данные, которые я загружаю из примеров, о которых я говорю здесь.Если вы посмотрите на ключ CustomerLog для первого набора данных, то это массив, однако если вы посмотрите на второй набор данных, это объект.
Я использую оператор for и вложенный цикл while.чтобы получить эту информацию.Но я получаю исключение из-за случая, о котором я упоминал, что CustomerLog иногда является массивом, а затем объектом.
Исключение выдается, потому что я запрашиваю JSON Array, используя метод getJSONArray, но этообъект.Я действительно нуждаюсь в помощи, поскольку я действительно застопорился и не могу найти никаких ответов на мою проблемуЛюбая помощь приветствуется.
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"
}
}