1

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)
6
  • 1
    json.getJSONArray("data") return a JSONArray, not a String Commented Mar 16, 2016 at 13:15
  • i think json above is not valid, a { at begin and } and end is missing Commented Mar 16, 2016 at 13:16
  • 1
    Not really sure at the moment, but I guess that there is some kind of "getList()" function. Consider carefully reading the error message first before posting it here...It says "not a string", you are calling "getString()". Kinda visible what the problem is here. Commented Mar 16, 2016 at 13:17
  • its a array inside an array. so be careful to fetch the data Commented Mar 16, 2016 at 13:28
  • you had initialised with numbers then how can you expect string from initialised data?? Commented Mar 16, 2016 at 13:43

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

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.