JSON Array iteration in Android/Java

后端 未结 8 995
广开言路
广开言路 2020-11-22 13:22

I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows

8条回答
  •  迷失自我
    2020-11-22 13:40

    You are using the same Cast object for every entry. On each iteration you just changed the same object instead creating a new one.

    This code should fix it:

    JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
    ArrayList castList= new ArrayList();
    
    for (int i=0; i < jCastArr.length(); i++) {
        Cast person = new Cast();  // create a new object here
        JSONObject jpersonObj = jCastArr.getJSONObject(i);
    
        person.castId = (String) jpersonObj.getString("id");
        person.castFullName = (String) jpersonObj.getString("name");
    
        castList.add(person);
    }
    details.castList = castList;
    

提交回复
热议问题