在Java

时间:2018-10-09 00:05:29

标签: java json org.json

json包,用于通过API处理我的JSON对象和数组。我的问题不一定是迭代,但是我下拉的某些数据实例是JSON Array,然后是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"
   }
}

2 个答案:

答案 0 :(得分:0)

您的意思是说,如果只有一个结果,那么API只会以一个对象而不是一个元素的数组作为响应?如果是这种情况,则说明API存在设计问题。应该清楚何时返回单个实例以及何时返回集合。

最快的黑客方式是处理catch块中的对象:

catch (Exception e) 
{
    Object log = ((org.json.JSONObject) customerlog)get("CustomerLog");
    // do somthing with your log here
}

答案 1 :(得分:0)

为什么不只检查customerLog是否是数组? if(Array.isArray(customerLog)),然后决定如何处理您的数据?