2014-02-28 115 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