1

所以,有这个 JSON 代码。我试图获得“abridged_cast”。但它很复杂。它在 JSONArray 内的 JSONObject onside jSONObject Inside JsonArray....

{
    "total": 591,
    "movies": [
        {
            "title": "Jack and Jill",
            "year": 2011,
            "runtime": "",
            "release_dates": {
                "theater": "2011-11-11"
            },
            "ratings": {
                "critics_score": -1,
                "audience_score": 90
            },
            "synopsis": "",
            "posters": {
                "thumbnail": "",
                "profile": "",
                "detailed": "",
                "original": ""
            },
            "abridged_cast": [
                {
                    "name": "Al Pacino",
                    "characters": []
                },
                {
                    "name": "Adam Sandler",
                    "characters": []
                },
                {
                    "name": "Katie Holmes",
                    "characters": []
                }
            ],
            "links": {
                "self": "",
                "alternate": ""
            }
        }
    ],
    "links": {
        "self": "",
        "next": ""
    },
    "link_template": ""
}

这是我获取“标题”和“年份”的代码

if (response != null) {
            try {
                // convert the String response to a JSON object,
                // because JSON is the response format Rotten Tomatoes uses
                JSONObject jsonResponse = new JSONObject(response);

                // fetch the array of movies in the response
                JSONArray movies = jsonResponse.getJSONArray("movies");

                // add each movie's title to an array
                movieTitles = new String[movies.length()];
                for (int i = 0; i < movies.length(); i++) {
                    JSONObject movie = movies.getJSONObject(i);
                    movieTitles[i] = movie.getString("title");
                }

希望有人能帮助我,因为我不知道如何获得 abridged_cast”

4

2 回答 2

0
try {
                 String Movie = null;
                 String abridged = null;
                JSONArray jsonResponse = new JSONArray(response);
                 for (int i = 0; i< jsonResponse.length(); i++) {
                        Movie     =   jsonResponse.getJSONObject(i).getString("movies").toString();
                        System.out.println("movies="+Movie);
                       abridged =   jsonResponse.getJSONObject(i).getString("abridged_cast").toString();
                 }


                 JSONArray jArray = new JSONArray(Movie);
                 for (int i = 0; i< jArray.length(); i++) {
                     String title       =   jArray.getJSONObject(i).getString("title").toString();
                     System.out.println("title="+title);

                 }

                JSONArray jabridgeArray = new JSONArray(abridged);
                 for (int i = 0; i< jabridgeArray.length(); i++) {
                     String title       =   jabridgeArray.getJSONObject(i).getString("name").toString();
                     System.out.println("title="+title);

                 }

            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
于 2013-06-13T12:18:57.753 回答
0

movies包含一组“电影”对象。这些对象中的每一个都包含一个字段,该字段abridged_cast是一个(我们称它们为“演员”)对象数组。

如果您不打算映射到 POJO 而是通过 JSON,您只需在 get 之后在循环中获取该数组movie,并使用另一个循环以相同的方式从该数组中获取每个“演员表成员”对象.

...
JSONArray cast = movie.getJSONArray("abridged_cast");
for (int j = 0; j < cast.length(); j++) {
    JSONObject castMember = cast.getJSONObject(j);
    ... 
}

从评论中编辑:您最初的问题涉及如何从您拥有的 JSON 中提取信息;上面的代码解释了这一点。现在,您似乎在问一个关于如何使用它的更基本的编程问题。

如果您打算使用org.jsonAndroid 附带的包含类,您现在知道如何访问返回的 JSON 对象中的信息。您可以在已解析对象周围编写方法,JSONObject以使用包中的对象和方法按原样访问数据json.org。例如,您可以编写一个“getMovie()”方法,将电影名称作为字符串,并在“movies”数组中搜索正确的,并将其作为JSONObject.

通常,您会在 Java 中创建封装在该 JSON 中返回的数据的类,并使用适合您的访问模式的数据结构(例如,包含Map所有使用电影名称作为键的电影)。使用这些org.json类,您必须实例化这些对象并在解析 JSON 时手动填充它们,就像您在问题中所做的那样。如果您使用GsonJacksonJSON 解析库,它们能够获取您拥有的 JSON 并将所有数据映射到您创建的类并在一次调用中返回它们。

于 2013-06-13T12:39:09.490 回答