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

无法使用GSON将JSON字符串转换为JAVA对象,java,json,gson,Java,Json,Gson,我有一个json文件,其中保存了特定频道的时间表。现在,我想使用gson将字符串转换为java对象。我知道这是非常琐碎的,但我对字符串的结构感到困惑。这是我的JSON字符串的格式: { "date": "28022014", "channelName": "star-movies", "listOfShows": [ { "showTitle": "Pirates of the Caribbean: The Curse of the Black Pearl",

我有一个
json
文件,其中保存了特定频道的时间表。现在,我想使用gson将
字符串
转换为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;
    }

}
公共类频道时间表{
私有字符串日期;
私有字符串名;
展示的私人列表;
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getChannelName(){
返回信道名称;
}
public void setChannelName(字符串channelName){
this.channelName=channelName;
}
公共列表getListOfShows(){
返回显示列表;
}
公共无效设置列表显示(列表显示){
this.listOfShows=listOfShows;
}
}
公开课{
私有字符串显示标题;
私有字符串显示时间;
私人字符串显示拇指;
公共字符串getShowThumb(){
返回显示拇指;
}
公共无效设置showThumb(字符串showThumb){
this.showtumb=showtumb;
}
公共字符串getShowTitle(){
返回showTitle;
}
公共void setShowTitle(字符串showTitle){
this.showTitle=showTitle;
}
公共字符串getShowTime(){
返回显示时间;
}
公共void设置显示时间(字符串显示时间){
this.showTime=showTime;
}
}

我无法确定如何在列表中客观化
showDetails
?因为它的键值对之间有空格。。。?我是不是遗漏了什么?或者有解决方法吗?

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

您有两个选项

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

public class ShowDetails {

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

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

...
ShowDetails showDetails;
...
但这可能有点疯狂,这取决于有多少东西可以在那里,或者如果你不知道所有的可能性是什么。选项B更简单,请使用
地图

公共类节目{
私有字符串显示标题;
私有字符串显示时间;
私人字符串显示拇指;
私人地图展示详情;
// ...
}

JSON中的键/值对将作为。。。键/值对

你的问题是什么还不清楚。您在Java中建模JSON时是否遇到问题,或者在尝试反序列化时是否遇到错误?如果你问起建模的问题,我建议你使用非结构化的
Map
,因为它看起来像是可变的键值对。是的,建模是这样的,在这种情况下
Map
如何工作?。
gson
支持
Map
?你知道如何使用Java
Map
s吗?它们是标准库中相当基本的一部分;如果你不熟悉他们。您希望声明
映射showDetails
。在这种情况下不起作用,因为该对象是有关该节目的琐事的随机组合。我认为一张地图是他能得到的最好的。
public class Show {

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