2

I have the following structure for storing messages:

{
    "Channel_12": [
        ["[{\"to\":\"Bill\",\"msg\":\"Hello\",\"time\":\"10:36\"}]"], "[{\"agentName\":\"demo\",\"msg\":\"Hello 2\",\"time\":\"10:37\"}]"
    ],
    "Channel_34": [
        ["[{\"to\":\"Bill\",\"msg\":\"Hey 1\",\"time\":\"10:37\"}]"], "[{\"agentName\":\"demo\",\"msg\":\"Hey 2\",\"time\":\"10:38\"}]"
    ] }

I'm now trying to loop through and get each message for a given channel, lets say i want to fetch all the messages for channel Channel_12.

Currently i have the following code:

        org.codehaus.jettison.json.JSONArray c = obj.getJSONArray("Channel_12");

        for (int i = 0 ; i < c.length(); i++) {
            JSONObject obj = c.getJSONObject(i); <!-- Exception is here

            System.out.println(obj.getString("to").toString());
            System.out.println(obj.getString("msg").toString());

        }

But am getting the following exception:

org.codehaus.jettison.json.JSONException: JSONArray[0] is not a JSONObject.
    at org.codehaus.jettison.json.JSONArray.getJSONObject(JSONArray.java:277)
    at im.Client$HistoryJSON.getHistory(Client.java:7229)
    at im.Client$32$4.run(Client.java:2246)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Any ideas?

1
  • Why are you storing escaped JSON within JSON? That makes little sense. If you store it without escaping it, then your code would work i.e. { "Channel_12":[{"to":"Bill"... etc Commented Dec 3, 2013 at 11:23

2 Answers 2

1

Chage your code like,

        JSONArray c = jsonObject.getJSONArray("Channel_12");  //Change your code from here
        JSONArray array = c.getJSONArray(0);
        String data = c.getString(1);
        Log.i("#Values", array.getString(0) + "\t" + data);

See my result,

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This fixes the sample, hence I upvoted, but really the sample is flawed.
0

It looks like your JSONArray contains a JSONArray and a JSONObject. A JSONArray is not a JSONObject.

Are you in control of the source format? Because it looks a little weird.

The structure is as so: At position 0 is an array with one object, and a position 1 is an object.

Your code can be changed to this:

org.codehaus.jettison.json.JSONArray c = obj.getJSONArray("Channel_12");
JSONArray array = c.getJSONArray(0);
JSONObject obj2 = array.getJSONObject(0);

System.out.println(obj2.getString("to").toString());
System.out.println(obj2.getString("msg").toString());

4 Comments

Getting: org.codehaus.jettison.json.JSONException: JSONArray[0] is not a JSONArray.
Can you prettyprint the c variable and see what it thinks is in it?
How can i accomplish that?
I suppose just clicking on it in the debugger would get you the info you need.

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.