org.json.JSONException: JSONArray[0] not a string

Hi i am trying to parse json My json looks like:

"data":[[0,1,0],[0,2,0],[0,3,0],[0,4,0],[0,5,0]]

i am using following code to perform action

int k=0;
for (int j = 1; j <= 7; j++)
                {
                    String data = json.getJSONArray("data").getString(k);
                    data = data.substring(1, data.length() - 1);
                    String[] countArr = data.split(",");
}

Following Excetion i am geting;

org.json.JSONException: JSONArray[0] not a string.
    at org.json.JSONArray.getString(JSONArray.java:333)
    at com.inrev.analytics.manager.impl.IRticsManagerImpl.userActivity(IRFbAnalyticsManagerImpl.java:4276)
    at com.inrev.analytics.action.facebook.IRFsightsAction.pageUserActivity(IRFBInsightsAction.java:4004)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)

Answers


The error is quite clear. The first element is not a String but a JSONArray

Do something like this

JSONArray data = json.getJSONArray("data");

for (int i=0; i<data.length(); i++) {
    JSONArray element = data.getJSONArray(i);
    //Do something with your element
}

Need Your Help

Meteor publish/subscribe strategies for unique client-side collections

javascript mongodb meteor

Using Meteor, I'm wondering how best to handle different client-side collections that share the same server-side database collection. Consider the following example: I have a User collection, and ...