Java 无法用Android中json文件的内容填充列表
我试图将json文件的内容添加到arraylist中。我以前已经做过几次了,但是我不知道在这个特殊的情况下有什么错,我不能向arraylist添加任何内容 所以我有一个:Java 无法用Android中json文件的内容填充列表,java,android,arrays,json,Java,Android,Arrays,Json,我试图将json文件的内容添加到arraylist中。我以前已经做过几次了,但是我不知道在这个特殊的情况下有什么错,我不能向arraylist添加任何内容 所以我有一个: private List<Person> persons = new ArrayList<Person>(); 并写下: 然后它将被添加到数组中 我想某处有个小错误,但我找不到解决办法 代码中几乎不需要任何更正。检查代码块中的注释 public void writeJson(){ tr
private List<Person> persons = new ArrayList<Person>();
并写下:
然后它将被添加到数组中
我想某处有个小错误,但我找不到解决办法 代码中几乎不需要任何更正。检查代码块中的注释
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();
}
}
这将解决您的问题。您只有一个Person对象-您在循环的每次迭代中都会更改该Person的属性,但会向列表中添加对同一对象的多个引用。这可能不是唯一的问题,但这是一个重要的问题。我还建议您在术语方面更加精确—您有一个列表,而不是数组。数组类似于Person[]。ArrayList是由数组内部支持的列表;因为loop@luckwithu是的,我以前试过,但它不起作用。不起作用,几乎没有给我们提供任何信息。怎么搞的?你是怎么诊断的?您是否已使用调试器逐步完成了代码?发生什么事了?谢谢你。这里已经很晚了,我就是想不出问题所在。非常感谢您指出这一点。您每次都将同一个人对象添加到列表中。因此,根据您的数据,所有人都有相同的数据code@GopalSinghSirvi这是不正确的,答案解决了问题。@RainMan Gopal提出的观点是正确的,以避免与对象引用相关的问题。我更新了代码以解决这个问题。@Ashwini哦,我明白了,我修改了代码,并在应用更改之前将此人带到了循环中,所以在我的情况下,它与前面的答案一起工作。
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();
}
}
persons.add(new Person("John", "23 years old", 1));
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();
}
}