org.json.JSONException:JSONArray [0]不是字符串

时间:2016-03-16 13:12:03

标签: java arrays json

您好我正在尝试解析json我的json看起来像:

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

我正在使用以下代码执行操作

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(",");
}

在Excetion之后我正在考虑;

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)

1 个答案:

答案 0 :(得分:0)

错误很明显。第一个元素不是String,而是JSONArray

做这样的事情

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

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