无法使用GSON将JSON字符串转换为JAVA对象

时间:2014-02-28 08:13:21

标签: java json gson

我有一个json文件,我在其中保存了特定频道的时间表。现在,我想使用gson将string转换为java对象。我知道这很简单,但有些事我对字符串的结构感到困惑。这是我的JSON字符串的格式:

{
  "date": "28022014",
  "channelName": "star-movies",
  "listOfShows": [
    {
      "showTitle": "Pirates of the Caribbean: The Curse of the Black Pearl",
      "showTime": "01:30:00",
      "showThumb": "http://tv.burrp.com/images/s/v/v/vv71wogh_644_4_140.jpg",
      "showDetails": {
        "IMDB Rating": "8.0/10",
        "Nominated For": "Bafta Film Award Best Performance by an Actor in 2004: Johnny Depp, Best Sound in 2004: Christopher Boyes; George Watters II, Best in Special Visual Effects: John Knoll; Hal T. Hickel",
        "Trivia": "The movie is inspired by, and takes its theme from, the popular Walt Disney theme park ride of the same name.",
        "Produced By": "Jerry Bruckheimer",
        "Directed By": "Gore Verbinski",
        "Show Type:": "Movie",
        "Followed By": "Pirates of the Caribbean: Dead Man\u0027s Chest",
        "Written By": "Ted Elliott, Terry Rossio",
        "Language:": "English",
        "Repeats on:": "Sun, Feb 23 11:30PM Tue, Feb 25 7:00AM Wed, Feb 26 2:00PM",
        "Music By": "Klaus Badelt",
        "Release Date": "9 July 2003",
        "Cast": "Johnny Depp, Geoffrey Rush, Orlando Bloom, Keira Knightley, Jack Davenport",
        "Genre:": "Action/Adventure Sci-Fi/Fantasy",
        "Show Description": "The Governor\u0027s beautiful daughter Elizabeth (Keira Knightley) is kidnapped by the evil Captain Barbossa (Geoffrey Rush). At that point, Will Turner (Orlando Bloom), her would-be suitor, seeks the help of Captain Jack Sparrow (Johnny Depp), a notorious conman. But then, Jack has his own reasons for hunting down Barbossa and his crew. They have stolen Sparrow\u0027s ship, The Black Pearl."
      }
    },
    {
      "showTitle": "Fillers",
      "showTime": "03:30:00",
      "showThumb": "http://tv.burrp.com/images/s/e/i/eims7nmh_1fwv_1_75.jpg",
      "showDetails": {
        "Repeats on:": "Sat, Feb 22 1:29AM Mon, Feb 24 3:30AM Tue, Feb 25 2:30AM",
        "Language:": "English",
        "Show Type:": "Promo/Filler",
        "Show Description": "It\u0027s a series featuring film based program."
      }
    },
    ....
    ...
  }]
 }

这是我提出的课程结构:

public class ChannelSchedule {

    private String date;
    private String channelName;
    private List<Show> listOfShows;
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getChannelName() {
        return channelName;
    }
    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }
    public List<Show> getListOfShows() {
        return listOfShows;
    }
    public void setListOfShows(List<Show> listOfShows) {
        this.listOfShows = listOfShows;
    }
}



 public class Show {

    private String showTitle;
    private String showTime;
    private String showThumb;

    public String getShowThumb() {
        return showThumb;
    }
    public void setShowThumb(String showThumb) {
        this.showThumb = showThumb;
    }
    public String getShowTitle() {
        return showTitle;
    }
    public void setShowTitle(String showTitle) {
        this.showTitle = showTitle;
    }
    public String getShowTime() {
        return showTime;
    }
    public void setShowTime(String showTime) {
        this.showTime = showTime;
    }

}

我无法弄清楚如何在列表中对showDetails进行客观化?因为它的关键值对之间有空间......?我在这里错过了什么吗?或者有解决方法吗?

2 个答案:

答案 0 :(得分:1)

您有两种选择;

看起来showDetails可能包含许多不同的内容。如果您知道它们是什么,可以映射出每一个可能的:

public class ShowDetails {

    @SerializedName("IMDB Rating")
    String imdbRating;
    @SerializedName("Repeats on:")
    String repeatsOn;
    // etc , etc

}

然后将其添加到您的Show课程中:

...
ShowDetails showDetails;
...

但这可能有点疯狂,取决于可以有多少东西,或者如果你不知道所有可能性是什么。选项B更简单,使用Map<String, String>

public class Show {

    private String showTitle;
    private String showTime;
    private String showThumb;
    private Map<String, String> showDetails;
    // ...
}

JSON中的键/值对将作为...键/值对放在地图中。

答案 1 :(得分:0)

您将ShowDetails建模为另一个类,并在Show类中具有ShowDetails属性。要使用空格和冒号符号引用属性,请使用anotations @“key”。