首页 > 解决方案 > java中JSON嵌套对象和数组的迭代

问题描述

json 包来处理来自 API 的 JSON 对象和数组。我的问题不一定是迭代,但是我要提取的一些数据实例是 JSON 数组,然后是 JSON 对象。下面是我正在下载的实例的数据,我在这里谈论。如果查看第一组数据的键 CustomerLog,它是一个数组,但是如果您查看第二组数据,它是一个对象。

for 语句和嵌套的 while 循环是我用来获取这些信息的。但是由于我提到的情况是 CustomerLog 有时是数组,然后有时是对象,所以我抛出了一个异常。

抛出异常是因为我使用 getJSONArray 方法请求 JSON 数组,但它是一个对象。我真的需要帮助,因为我真的在拖延,找不到任何问题的答案。任何帮助表示赞赏。

 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"
   }
}

标签: javajsonorg.json

解决方案


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

最快的 hacky 方法是处理你 catch 块中的对象:

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

推荐阅读