2014-02-28 76 views
0

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

{ 
    "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." 
     } 
    }, 
    .... 
    ... 
    }] 
} 

这是我想出了类结构:

我无法弄清楚如何物化在清单showDetails?由于它的主要价值对之间有空间......?我在这里错过了什么吗?或者有没有解决方法?

+0

目前尚不清楚你的问题是什么。在Java中建模JSON时遇到问题吗?或者在尝试反序列化时遇到一些错误?如果你问的是建模,我会建议使用非结构化的Map,因为它看起来像是可变的键值对。 – chrylis

+0

是的,建模是,在这种情况下'maps'如何工作? – faizanjehangir

+0

..并'gson'支持'地图'? – faizanjehangir

回答

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值对将被放置在地图上的...键/值对。

0

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

+0

在这种情况下不起作用,因为该对象是关于该节目的随机分组的琐事。我认为'Map '是最好的。 – chrylis