如何解析这个JSON数据结构?

时间:2014-01-16 18:10:14

标签: android json parsing

我想从下面的JSON数据中解析以下项目:标题,年份,运行时,评论家共识,剧院发布日期,概要,海报缩略图和海报原件。但是当我解析我的数据时,我得到一个JSON异常:没有TAG_TITLE的值,基本上没有任何反应。

{"query": {
 "count": 30,
"created": "2014-01-16T14:39:33Z",
"lang": "en-US",
"results": {
 "movies": [
 {
 "id": "13065",
 "title": "Gladiator",
 "year": "2000.0",
 "mpaa_rating": "R",
 "runtime": "171.0",
 "critics_consensus": "Ridley Scott and an excellent cast successfully convey the intensity of Roman gladitorial combat as well as the political intrigue brewing beneath.",
 "release_dates": {
  "theater": "2000-05-05",
  "dvd": "2000-11-21"
 },
 "ratings": {
  "critics_rating": "Certified Fresh",
  "critics_score": "76.0",
  "audience_rating": "Upright",
  "audience_score": "87.0"
 },
 "synopsis": "",
 "posters": {
  "thumbnail": "http://content6.flixster.com/movie/11/16/89/11168944_mob.jpg",
  "profile": "http://content6.flixster.com/movie/11/16/89/11168944_pro.jpg",
  "detailed": "http://content6.flixster.com/movie/11/16/89/11168944_det.jpg",
  "original": "http://content6.flixster.com/movie/11/16/89/11168944_ori.jpg"
 },
 "abridged_cast": [
  {
   "name": "Russell Crowe",
   "id": "162652569",
   "characters": "Maximus"
  },
  {
   "name": "Joaquin Phoenix",
   "id": "162655394",
   "characters": "Commodus"
  },
  {
   "name": "Connie Nielsen",
   "id": "162653203",
   "characters": "Lucilla"
  },
  {
   "name": "Oliver Reed",
   "id": "162662908",
   "characters": "Proximo"
  },
  {
   "name": "Derek Jacobi",
   "id": "162656362",
   "characters": "Gracchus"
  }
 ],
 "alternate_ids": {
  "imdb": "0172495"
 },
 "links": {
  "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/13065.json",
  "alternate": "http://www.rottentomatoes.com/m/gladiator/",
  "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/13065/cast.json",
  "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/13065/clips.json",
  "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/13065/reviews.json",
  "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/13065/similar.json"
 }
}

这是用于提取我想要的数据的代码:

            jsonObject = new JSONObject(result);

            JSONObject queryJSONObject = jsonObject.getJSONObject("query");

            JSONObject resultsJSONObject = queryJSONObject.getJSONObject("results");

            JSONArray moviesJSONArray = resultsJSONObject.getJSONArray("movies");

            for(int i = 0; i < moviesJSONArray.length(); i++){
                JSONObject c = moviesJSONArray.getJSONObject(i);


                String title = c.getString("TAG_TITLE");
                String year = c.getString("TAG_YEAR");
                String mpaa_rating = c.getString("TAG_RATING");
                String runtime = c.getString("TAG_RUNTIME");
                String critics_consensus = c.getString("TAG_CONSENSUS");

                JSONObject dates = c.getJSONObject("release_dates");
                String theater = dates.getString("TAG_THEATER");

                JSONObject posters = c.getJSONObject("posters");
                String thumbnail_posters = posters.getString("TAG_TBPOSTER");
                String original_posters = posters.getString("TAG_ORIPOSTER");

                Log.e(TAG, 
                         ", Title: " + title
                        + ", Year: " + year
                        + ", Rating: " + mpaa_rating
                        + ", Runtime: " + runtime
                        + ", Consensus :" + critics_consensus
                        + ", Theater date: " + theater
                        + ". Poster small: " +  thumbnail_posters
                        + ", Poster big: " + original_posters
                        );

                HashMap<String, String> movie = new HashMap<String, String>();

                movie.put(TAG_TITLE, title);
                movie.put(TAG_YEAR, year);
                movie.put(TAG_RATING, mpaa_rating);
                movie.put(TAG_RUNTIME, runtime);
                movie.put(TAG_CONSENSUS, critics_consensus);
                movie.put(TAG_THEATER, theater);
                movie.put(TAG_TBPOSTER, thumbnail_posters);
                movie.put(TAG_ORIPOSTER, original_posters);

                moviesList.add(movie);

在我的OnCreate之前,我正在使用这些字符串作为TAGS:

private static final String TAG_CONSENSUS = "critics_consensus";
private static final String TAG_RATING = "mpaa_rating";
private static final String TAG_YEAR = "year";
private static final String TAG_THEATER = "theater";
private static final String TAG_TITLE = "title";
private static final String TAG_RUNTIME = "runtime";
private static final String TAG_DATE = "theater";
private static final String TAG_TBPOSTER = "thumbnail_posters";
private static final String TAG_ORIPOSTER = "original_posters";

JSONArray movies = null;
ArrayList<HashMap<String, String>> moviesList;

我的TAG_TITLE是“标题”,它也用在JSON数据中,所以我不知道为什么我会收到此错误?或者我的代码错了?提前致谢

2 个答案:

答案 0 :(得分:1)

不应该这样做

String title = c.getString(TAG_TITLE);

而不是

String title = c.getString("TAG_TITLE");

答案 1 :(得分:1)

你应该像

一样直接写String
 JSONObject jsonobject = new JSONObject(jsonString);
 JSONObject queryjson = jsonobject.getJSONObject("query");
 String count = queryjson.getString("count");

OR

 //This Code is As per Your Coding
 JSONObject jsonobject = new JSONObject(jsonString);
 JSONObject queryjson = jsonobject.getJSONObject(TAG_QUERY);
 String count = queryjson.getString(TAG_COUNT);

Inshort只需在字符串名称 TAG _ ..

之前或之后删除“”