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

标签 java json gson

我有一个 json 文件,其中保存了特定 channel 的时间表。现在,我想使用 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 ?因为它的键值对之间有空格......?我在这里错过了什么吗?或者有解决方法吗?

最佳答案

你有两个选择;

看起来像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 中的键/值对将作为...键/值对放入映射中。

关于java - 无法使用 GSON 将 JSON 字符串转换为 JAVA 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22089667/

相关文章:

java - 排序 h :dataTable

java - 如何处理用户输入字符串中的撇号?

javascript - 更新 JSON 键值 - AngularJS

java - 如何在 Scala 中将复杂的 JSON 字符串转换为 MAP

java - GSON 序列化未返回正确的 json 字符串

使用 GSON 库和 ProGuard 时 Android 崩溃

java - GSON 将 String[] 反序列化为一种类型中的 String

java - Senserelate 目标字 : provide the "best" alternative for end-users

java - 在 Adempiere 的模型类中添加确认(是/否)对话框

javascript - 如何使用 Javascript 而不是 jQuery 从 HTML 表单数据生成 JSON 列表?