尝试从Rotten Tomatoes API解析JSON结果

时间:2013-11-05 22:57:34

标签: android json rotten-tomatoes

我成功获得电影的头衔和年份,但不知何故不是故事大纲。据我了解对象的形状,它们应该以相同的方式可达:

JSONObject resultOBJ = new JSONObject(result);
Log.v("hhhh",resultOBJ.toString());
JSONArray movArr = resultOBJ.getJSONArray("movies"); 
JSONObject movOBJ =movArr.getJSONObject(0);
String title = movOBJ.getString("title");
String synop = movOBJ.getString("synopsis");

nameView.setText(title);
synopView.setText(synop);

我收到的对象看起来像这样(没有格式化):

{
    "total": 1,
    "movies": [
        {
            "critics_consensus": "Deftly blending comedy, ...",
            "id": "770672122",
            "mpaa_rating": "G",
            "ratings": {
                "audience_rating": "Upright",
                "audience_score": 87,
                "critics_rating": "Certified Fresh",
                "critics_score": 99
            },
            "release_dates": {
                "dvd": "2010-11-02",
                "theater": "2010-06-18"
            },
            "runtime": 103,
            "title": "Toy Story 3",
            "year": 2010,
            "synopsis": "Pixar returns to their first success with ...

et cetra ...

1 个答案:

答案 0 :(得分:1)

我拿了你的例子(复制/粘贴),为Json关闭了一些门,得到了正确的结果:

public static void main(String[] args) throws JSONException  {

    String result = "{" + 
            "    \"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\"," + 
            "                \"audience_score\": 87" + 
            "            }," + 
            "            \"synopsis\": \"Pixar returns to their first success with Toy Story 3. The movie begins with Andy leaving for college and donating his beloved toys -- including Woody (Tom Hanks) and Buzz (Tim Allen) -- to a daycare. While the crew meets new friends, including Ken (Michael Keaton), they soon grow to hate their new surroundings and plan an escape. The film was directed by Lee Unkrich from a script co-authored by Little Miss Sunshine scribe Michael Arndt. ~ Perry Seibert, Rovi\"" + 
            "        }" + 
            "    ]" + 
            "}";


    JSONObject resultOBJ = new JSONObject(result);

    JSONArray movArr = resultOBJ.getJSONArray("movies"); 

    JSONObject movOBJ =movArr.getJSONObject(0);
    String title = movOBJ.getString("title");
    String synop = movOBJ.getString("synopsis");
    System.out.println(synop); 
}

输出:

  

皮克斯在玩具总动员3中首次获得成功。电影开始于安迪离开大学并捐赠他心爱的玩具 - 包括伍迪(汤姆汉克斯)和巴兹(蒂姆艾伦) - 到日托。当船员遇到包括肯(迈克尔基顿)在内的新朋友时,他们很快就会厌恶他们的新环境并计划逃跑。这部电影是由Lee Unkrich执导的,由Little Miss Sunshine抄写员Michael Arndt共同撰写。 ~Pervi的Perry Seibert