0

我无法解析这个 JSON。我在 JSONObject o 中有以下数据。我如何获取critics_score 的值?

{
"total":1,"
 movies":
 [{
      "id":"770672122",
      "title":"Toy Story 3",
      "year":2010,
      "mpaa_rating":"G",
      "runtime":103,
      "critics_consensus":"Deftly blending comedy, adventure, and honest emotion, Toy   Story 3 is a rare second sequel that really works.",

      "release_dates":{"theater":"2010-06-18","dvd":"2010-11-02"},
      "ratings":
       {
                "critics_rating":"Certified Fresh",
                "critics_score":99,
                "audience_rating":"Upright", .......

谢谢

4

4 回答 4

2

You could do the following:

    int criticsScore;
    try {
        criticsScore = myJsonObject.getJSONArray("movies").getJSONObject(0).getJSONObject("ratings").getInt("critics_score");
    } catch (JSONException e) {
        e.printStackTrace();
    }

Edit: This is assuming your JSONObject is named myJsonObject.

于 2013-05-20T23:58:30.667 回答
1

data_array['movies'][0]['ratings']['critics_score']

使用它来帮助阅读您的 JSON - http://jsoneditoronline.org/

要创建对象 data_array 我使用了这个, var data_array = $.parseJSON( '{"total":1,"movies":[{"id":"770672122","title":"Toy Story 3","year ":2010,"mpaa_rating":"G","runtime":103,"critics_consensus":"《玩具总动员 3》巧妙地融合了喜剧、冒险和真诚的情感,是难得一见的第二部续作。","re​​lease_dates" :{"theater":"2010-06-18","dvd":"2010-11-02"},"ratings":{"critics_rating":"Certified Fresh","critics_score":99,"audience_rating" :“直立”}}]}');

于 2013-05-21T00:04:52.077 回答
1

看看我写的关于在Android平台解析JSON文件的这篇博文,你可能会觉得很方便:

JSON解析

作为一个方向,您需要将movies对象解析为 a JSONArray,然后获取它的第一项并提取评级JSONObject,然后从中提取critics_score int

于 2013-05-20T23:55:45.120 回答
0

Gson 库对于解析 JSON 非常有帮助:

https://code.google.com/p/google-gson/

上面 Emil 的教程可以帮助你入门

其他人建议杰克逊,所以这是一个比较它们的stackoverflow线程

于 2013-05-21T01:22:51.640 回答