预计BEGIN_OBJECT但是STRING - json没有键

时间:2014-11-22 21:10:09

标签: java android json

我一般都是Android和json的初学者,而且我只是坚持一部分而需要一些帮助。我试图使用烂番茄json。我管理了几乎所有内容,除了json中没有键的部分。这是json:



{
  "movies": [{
    "id": "771359313",
    "title": "Dumb and Dumber To",
    "year": 2014,
    "mpaa_rating": "PG-13",
    "runtime": 109,
    "critics_consensus": "",
    "release_dates": {
      "theater": "2014-11-14"
    },
    "ratings": {
      "critics_rating": "Rotten",
      "critics_score": 25,
      "audience_rating": "Spilled",
      "audience_score": 54
    },
    "synopsis": "Jim Carrey and Jeff Daniels reprise their signature roles as Lloyd and Harry in the sequel to the smash hit that took the physical comedy and kicked it in the nuts: Dumb and Dumber To. The original film's directors, Peter and Bobby Farrelly, take Lloyd and Harry on a road trip to find a child Harry never knew he had and the responsibility neither should ever, ever be given. The Farrelly brothers produce Dumb and Dumber To alongside Riza Aziz and Joey McFarland of Red Granite Pictures. They are joined by fellow producers Charles B. Wessler and Bradley Thomas. Universal Pictures will distribute the film in the United States, Canada and select international territories.(C) Universal",
    "posters": {
      "thumbnail": "http://content6.flixster.com/movie/11/17/88/11178864_tmb.jpg",
      "profile": "http://content6.flixster.com/movie/11/17/88/11178864_tmb.jpg",
      "detailed": "http://content6.flixster.com/movie/11/17/88/11178864_tmb.jpg",
      "original": "http://content6.flixster.com/movie/11/17/88/11178864_tmb.jpg"
    },
    "abridged_cast": [{
      "name": "Jim Carrey",
      "id": "162659418",
      "characters": ["Lloyd Christmas"]
    }, {
      "name": "Jeff Daniels",
      "id": "162654392",
      "characters": ["Harry Dunne"]
    }, {
      "name": "Kathleen Turner",
      "id": "162658757",
      "characters": ["Fraida Felcher"]
    }, {
      "name": "Rob Riggle",
      "id": "770750133",
      "characters": ["Travis/Captain Lippincott"]
    }, {
      "name": "Jennifer Lawrence",
      "id": "770800260",
      "characters": ["Young Fraida Felcher"]
    }],
    "alternate_ids": {
      "imdb": "2096672"
    },
    "links": {
      "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771359313.json",
      "alternate": "http://www.rottentomatoes.com/m/dumb_and_dumber_to/",
      "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/771359313/cast.json",
      "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/771359313/reviews.json",
      "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/771359313/similar.json"
    }
  }, {
    "id": "771355766",
    "title": "Big Hero 6",
    "year": 2014,
    "mpaa_rating": "PG",
    "runtime": 93,
    "critics_consensus": "",
    "release_dates": {
      "theater": "2014-11-07"
    },
    "ratings": {
      "critics_rating": "Certified Fresh",
      "critics_score": 89,
      "audience_rating": "Upright",
      "audience_score": 94
    },
    "synopsis": "With all the heart and humor audiences expect from Walt Disney Animation Studios, \"Big Hero 6\" is an action-packed comedy-adventure about robotics prodigy Hiro Hamada, who learns to harness his genius-thanks to his brilliant brother Tadashi and their like-minded friends: adrenaline junkie Go Go Tamago, neatnik Wasabi, chemistry whiz Honey Lemon and fanboy Fred. When a devastating turn of events catapults them into the midst of a dangerous plot unfolding in the streets of San Fransokyo, Hiro turns to his closest companion-a robot named Baymax-and transforms the group into a band of high-tech heroes determined to solve the mystery. (C) Disney",
    "posters": {
      "thumbnail": "http://content7.flixster.com/movie/11/17/85/11178581_tmb.jpg",
      "profile": "http://content7.flixster.com/movie/11/17/85/11178581_tmb.jpg",
      "detailed": "http://content7.flixster.com/movie/11/17/85/11178581_tmb.jpg",
      "original": "http://content7.flixster.com/movie/11/17/85/11178581_tmb.jpg"
    },
    "abridged_cast": [{
      "name": "Ryan Potter",
      "id": "771360315",
      "characters": ["Hiro Hamada"]
    }, {
      "name": "Scott Adsit",
      "id": "406975480",
      "characters": ["Baymax"]
    }, {
      "name": "T.J. Miller",
      "id": "770690115",
      "characters": ["Fred"]
    }, {
      "name": "Jamie Chung",
      "id": "770694653",
      "characters": ["Go Go Tomago"]
    }, {
      "name": "Damon Wayans Jr.",
      "id": "770822247",
      "characters": ["Wasabi"]
    }],
    "alternate_ids": {
      "imdb": "2245084"
    },
    "links": {
      "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771355766.json",
      "alternate": "http://www.rottentomatoes.com/m/big_hero_6/",
      "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/771355766/cast.json",
      "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/771355766/reviews.json",
      "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/771355766/similar.json"
    }
  }],
  "links": {
    "self": "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=2&country=us",
    "alternate": "http://www.rottentomatoes.com/movie/box-office/"
  },
  "link_template": "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit={num-results}&country={country-code}"
}




现在,我设法得到名字,身份,但不是人物。以下是代码的一些部分,我如何使其工作:



public class Movie {
  String actor_name;
  public Movie(String response, int index) {
    Gson gson = new Gson();
    MoviesContainer movie = gson.fromJson(response.trim(), MoviesContainer.class);
    List < MovieData > movies = movie.movie
    AbridgedCastData[] aCast = gson.fromJson(gson.toJson(movies.get(index).abridged_cast), AbridgedCastData[].class);
    actor_name = aCast[0].name;
  }
}

class MovieData {
  Object abridged_cast;
}

class MoviesContainer {
  public List < MovieData > movies;
}

class AbridgedCastData {
  String name;
  String id;
  Object characters;
}
&#13;
&#13;
&#13;

我直接使用索引进行测试,因此actor_name运行良好。

我试过这样:

CharactersData[] character = gson.fromJson(gson.toJson(aCast[0].characters), CharactersData[].class);

但我收到错误:

Expected BEGIN_OBJECT but was STRING

我到处搜索,但我无法找到如何访问角色。如何获得Lloyd Christmas的角色?提前谢谢。

2 个答案:

答案 0 :(得分:1)

首先,您的JSON文件无效。它应以{开头,以}结尾。您可以使用JSONLint等在线验证工具进行核对。这就是您收到此错误的原因。

其次,如果您查看文件的结构,您会看到有一个名为abridged_cast的数组,其中包含一些对象(您在AbridgedCastData类中定义的很好)。

所以最终的结构可能是:

class A {
   //contains a list of objects B or an array of objects B
}

class B {
  //name
  //id
  //characters (list of Strings or String array)
}

我们差不多完成了!

如果您没有指定SerializedName注释,则解析器要求您要序列化的属性的字段名称必须与JSON文件中的相同(否则您最终会得到一个非初始化值)。由于情况并非如此,您需要添加@SerializedName("abridged_cast")注释。

最后你最终得到:

public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        BufferedReader br = new BufferedReader(new FileReader("myJson.json"));
        Gson gson = new Gson();
        MoviesContainer movie = gson.fromJson(br, MoviesContainer.class);
        List<AbridgedCastData> movies = movie.movies;
        System.out.println(movies);
    }
}

class MoviesContainer {
    @SerializedName("abridged_cast")
    public List<AbridgedCastData> movies;
}

class AbridgedCastData {
    String name;
    String id;
    List<String> characters;
    @Override
    public String toString() {
        return "AbridgedCastData [name=" + name + ", id=" + id
                + ", characters=" + characters + "]";
    }    
}

哪个输出:

[AbridgedCastData [name=Jim Carrey, id=162659418, characters=[Lloyd Christmas]], AbridgedCastData [name=Jeff Daniels, id=162654392, characters=[Harry Dunne]], AbridgedCastData [name=Kathleen Turner, id=162658757, characters=[Fraida Felcher]], AbridgedCastData [name=Rob Riggle, id=770750133, characters=[Travis/Captain Lippincott]], AbridgedCastData [name=Jennifer Lawrence, id=770800260, characters=[Young Fraida Felcher]]]

答案 1 :(得分:0)

您可以更改您的回复,然后解析它。

response = "{" + response + "}" ;
// now parse json