【问题标题】:Cannot fill the list with content of json file in Android无法在 Android 中用 json 文件的内容填充列表
【发布时间】:2015-07-23 08:09:11
【问题描述】:

我正在尝试将 json 文件的内容添加到 arraylist。我之前已经这样做了几次,但我无法弄清楚在这种特殊情况下出了什么问题,我无法将任何东西添加到数组列表中。

所以我有一个:

private List<Person> persons = new ArrayList<Person>();

这就是我从资产文件夹 (from this answer)加载 json 文件的方式:

public String loadJSONFromAsset() {
        String json = null;
        try {
            // json file name
            InputStream is = this.getAssets().open("file.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

写作

public void writeJson(){
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray response = new JSONArray(loadJSONFromAsset());


        for (int i = 0; i < response.length(); i++) {
            Person person = new Person();
            JSONObject jo_inside = response.getJSONObject(i);

            Log.d(TAG, jo_inside.getString("firstName"));

            //Add values in `ArrayList`
            person.setName(obj.getString("firstName"));
            person.setAge(obj.getString("age"));
            person.setPhoto(obj.getInt("id"));
            // Add to the array
            persons.add(person);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

当我尝试打印 person 数组的内容以进行测试时,使用上述方法我什么也得不到。但是,如果我插入这样的人:

persons.add(new Person("John", "23 years old", 1)); 

然后它将被添加到数组中。

我认为某处有一个小错误,但我找不到解决方案。

【问题讨论】:

  • 您只有一个 Person 对象 - 您在循环的每次迭代中更改该 Person 的属性,但将多个对同一对象的引用添加到列表中。这可能不是唯一的问题,但它是一个重要的问题。
  • 我还建议您在术语方面更加准确 - 您有一个列表,而不是一个数组。数组类似于Person[]。 (ArrayList 是一个由内部数组支持的列表。)
  • 把这一行 Person person = new Person();在 for 循环中
  • @luckwithu 是的,我之前尝试过,但没有成功
  • “它不起作用”为我们提供了几乎为零的信息。发生了什么?您尝试过什么诊断?您是否使用调试器单步执行了代码?发生了什么?

标签: java android arrays json


【解决方案1】:

您的代码中几乎不需要更正。检查代码块中的 cmets。

    public void writeJson(){
    try {
        //this line is not needed, since you are reading array & not not json object 
        //JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray response = new JSONArray(loadJSONFromAsset());

        for (int i = 0; i < response.length(); i++) {
            JSONObject jo_inside = response.getJSONObject(i);

            Log.d(TAG, jo_inside.getString("firstName"));

            //Add values in `ArrayList`
            //for setting the values use object read from response array 
            Person person = new Person();
            person.setName(jo_inside.getString("firstName"));
            person.setAge(jo_inside.getString("age"));
            person.setPhoto(jo_inside.getInt("id"));
            // Add to the array
            persons.add(person);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } 
}

这将解决您的问题。

【讨论】:

  • 谢谢。这里是深夜,我只是无法弄清楚问题所在。非常感谢您指出。
  • 您每次都将相同的人员对象添加到列表中。所以根据你的代码,所有人都有相同的数据
  • @GopalSinghSirvi 不正确,答案解决了问题。
  • @RainMan Gopal 提出的观点是正确的,以避免与对象引用相关的问题。我更新了代码来解决它。
  • @Ashwini 哦,我明白了,我修改了代码并将 Person 带入循环中,然后再应用您的更改,因此在我的情况下,它与以前的答案一起使用。
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多