简体   繁体   English

Android迭代包含JSON对象的JSON数组

[英]Android Iterating JSON Array containing JSON Object

So i have this code which basically takes a value from JSON array (containing several objects) and assign it appropriately. 所以我有这段代码,它基本上从JSON数组(包含几个对象)中获取一个值,并适当地分配它。

// RETRIEVE CAST LIST
JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
Cast person = new Cast();
ArrayList<Cast> castList= new ArrayList<Cast>();

for (int i=0; i < jCastArr.length(); i++) {
    JSONObject jpersonObj = jCastArr.getJSONObject(i);

    person.castId = (String) jpersonObj.getString("id");
    person.castFullName = (String) jpersonObj.getString("name");

    castList.add(person);
}
details.castList = castList;

The JSON value (rotten tomatoes) JSON值(烂番茄)

{
    "id": 771267761,
    "title": "Riddick",
    "year": 2013,
    "genres": [
        "Action & Adventure",
        "Science Fiction & Fantasy"
    ],
    "mpaa_rating": "R",
    "runtime": 119,
    "critics_consensus": "It may not win the franchise many new converts, but this back-to-basics outing brings Riddick fans more of the brooding sci-fi action they've come to expect.",
    "release_dates": {
        "theater": "2013-09-06"
    },
    "ratings": {
        "critics_rating": "Rotten",
        "critics_score": 57,
        "audience_rating": "Upright",
        "audience_score": 66
    },
    "synopsis": "Blah.....",
    "posters": {
        "thumbnail": "http://content8.flixster.com/movie/11/17/20/11172082_mob.jpg",
        "profile": "http://content8.flixster.com/movie/11/17/20/11172082_pro.jpg",
        "detailed": "http://content8.flixster.com/movie/11/17/20/11172082_det.jpg",
        "original": "http://content8.flixster.com/movie/11/17/20/11172082_ori.jpg"
    },
    "abridged_cast": [
        {
            "name": "Vin Diesel",
            "id": "162652472",
            "characters": [
                "Riddick"
            ]
        },
        {
            "name": "Karl Urban",
            "id": "162654704",
            "characters": [
                "Vaako"
            ]
        },
        {
            "name": "Jordi Molla",
            "id": "364617086",
            "characters": [
                "Santana"
            ]
        },
        {
            "name": "Matt Nable",
            "id": "771069067",
            "characters": [
                "Boss Johns"
            ]
        },
        {
            "name": "Katee Sackhoff",
            "id": "459518520",
            "characters": [
                "Dahl"
            ]
        }
    ],
    "abridged_directors": [
        {
            "name": "David Twohy"
        }
    ],
    "studio": "Universal Classics",
    "alternate_ids": {
        "imdb": "1411250"
    },
    "links": {
        "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771267761.json",
        "alternate": "http://www.rottentomatoes.com/m/riddick/",
        "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/771267761/cast.json",
        "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/771267761/clips.json",
        "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/771267761/reviews.json",
        "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/771267761/similar.json"
    }
}

The problem is when i call it like such 问题是当我这样称呼它时

ArrayList<Cast> list = details.castList;
Cast actor = list.get(0);
String temp = actor.castFullName;
longToast(temp);

It will always return Katee Sackhoff (no matter what index position it is). 它将始终返回Katee Sackhoff(无论它处于什么索引位置)。 I've tried to iterate it using for loops, but i just want to keep it simple for debugging purposes. 我尝试使用for循环对其进行迭代,但我只想使其简单以用于调试。

You are using the same Cast object for every entry. 每个条目都使用相同的Cast对象。
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<Cast> castList= new ArrayList<Cast>();

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM