0

json package to process my JSON objects and arrays from an API. My issue is not necessarily iteration however some instances of the data I am pulling down they are JSON Array and then JSON objects. Below is the data I am downloading of the instances which I am talking about here. If look at the key CustomerLog for the first set of data it is an Array however if you look at the second set of data it is an object.

The for statement and the nested while loop is what I am using to get this information. But I am getting an exception thrown because of the case I am mentioning that is CustomerLog is array sometimes and then an object sometimes.

The exception is thrown because I am asking for JSON Array using the getJSONArray method but it is an object. I really need assistance as I am really stalling and cannot find any answers to my problem. Any help is appreciated.

 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 Answers 2

0

Do you mean that if there's only one result, the API would just response with an object instead of an array with one element? If that's the case, the API has a design issue. It should be clear when to return a single instance and when to return a collection.

The fastest hacky way is to process the object in you catch block:

catch (Exception e) 
{
    Object log = ((org.json.JSONObject) customerlog)get("CustomerLog");
    // do somthing with your log here
}
Sign up to request clarification or add additional context in comments.

Comments

0

Why not just check if customerLog is an array? if(Array.isArray(customerLog)) and then decide what to do with your data?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.