JsonArray错误:org.json.JSONException:JSONArray [1]未找到 [英] JsonArray error : org.json.JSONException: JSONArray[1] not found

查看:2586
本文介绍了JsonArray错误:org.json.JSONException:JSONArray [1]未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用JSON新面临的一个问题。我搜索了很多,但无法找到答案吧!

I am currently new in Json and faced to a problem. I Searched a lot but could not find an answer to it!

我从一个JSON网址获得的名单。名称可以在此JSON文件中重复,但我只想要其中的一个记录保持到新阵列我把它叫做改编。你可以看到code如下:

I am getting a list of names from a json url. names can be duplicated in this json file but i only want to keep one record of them to my new array which i called "arr". You can see the code as following:

    JSONArray interests = json.getJSONArray("Interests");
    JSONArray arr = new JSONArray();
    int i = 0;
    int p = 0;
    int e = 0;

    for (; i < interests.length(); i++) {

        JSONArray items = interests.getJSONObject(i).getJSONArray("items");
        for (int j = 0; j < items.length(); j++) {
            String string = items.getJSONObject(j).getString("authors");
            String[] parts = string.split(",");
            for (int k = parts.length - 1; k >= 0; k--) {

                for (int a = 0; a <= arr.length(); a++) {

                    if (arr.length() == 0 || !arr.getJSONObject(a).getString("label").equals(parts[k])) {
                        JSONObject obj = new JSONObject();
                        obj.put("id", p++);
                        // obj.put("value", e++);
                        obj.put("label", parts[k]);
                        arr.put(obj);
                    }
                }
            }

        }

    }

    System.out.print(arr);
}

问题是当我运行此code我得到以下错误:

Problem is when i run this code I get the following error :

异常线程mainorg.json.JSONException:JSONArray [1]没有找到

Exception in thread "main" org.json.JSONException: JSONArray[1] not found.

我试图打印arr.length()在每次迭代中,我也得到1!但我真的不知道我为什么收到此错误?!

I tried to print arr.length() in each iteration and I get 1!! but I do not really know why i do receive this error?!

在此先感谢

推荐答案

指数转移的常见故障。下面的例子:

A common fault of index shifting. Following example:

Array[0] = "foo";
Array[1] = "bar";
for(int i=0; i <= Array.length(); i++)
  doSomesing(Array[i]);

Array.length()将返回2,但指数的愤怒是0到1,所以正确的是

Array.length() will return 2, but index rage is 0 to 1, so the correct would be

for(int i=0; i < Array.lenth(); i++)

这篇关于JsonArray错误:org.json.JSONException:JSONArray [1]未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆