2013-02-17 175 views
2

我是JSON中的新成員,並且在爲後續的JSON輸入創建POJO時遇到問題。嵌套json的POJO

[ 
    { 
"score":1, 
"popularity":3, 
"name":"Brad Pitt", 
"id":287, 
"biography":"test", 
"url":"http://www.themoviedb.org/person/287", 
"profile":[ 
    { 
     "image":{ 
      "type":"profile", 
      "size":"thumb", 
      "height":68, 
      "width":45, 
      "url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg", 
      "id":"4ea5cb8c2c0588394800006f" 
     } 
    }, 
    { 
     "image":{ 
      "type":"profile", 
      "size":"profile", 
      "height":281, 
      "width":185, 
      "url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg", 
      "id":"4ea5cb8c2c0588394800006f" 
     } 
    }, 
    { 
     "image":{ 
      "type":"profile", 
      "size":"h632", 
      "height":632, 
      "width":416, 
      "url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg", 
      "id":"4ea5cb8c2c0588394800006f" 
     } 
    }, 
    { 
     "image":{ 
      "type":"profile", 
      "size":"original", 
      "height":1969, 
      "width":1295, 
      "url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg", 
      "id":"4ea5cb8c2c0588394800006f" 
     } 
    } 
], 
"version":685, 
"last_modified_at":"2013-02-16 07:11:15 UTC" 
    } 
    ] 

我已經創建了三個POJO對他們的這個人是主要負責人類這其中蘊含配置文件對象也。簡介對象返回圖像列表:

public class Person implements Serializable { 

    private static final long serialVersionUID = 6794898677027141412L; 

    public String biography; 
    public String id; 
    public String last_modified_at; 
    public String name; 
    public String popularity; 
    public String score; 
    public String url; 
    public String version; 
    public ArrayList<Profile> profile; 

    //getters and setters 
    } 

    public class Profile implements Serializable { 

    private static final long serialVersionUID = -6482559829789740926L; 

    public ArrayList<Image> image; 

    } 


    public class Image implements Serializable { 

    private static final long serialVersionUID = -2428562977284114465L; 

    public String type; 
    public String url; 
    public String size; 
    public int width; 
    public int height; 
    } 

我使用GSON檢索數據:

Gson gson = new Gson(); 
    Type collectionType = new TypeToken<List<Person>>(){}.getType(); 
    List<Person> details = gson.fromJson(json, collectionType); 

當我運行此我得到:

02-17 11:04:50.531: E/AndroidRuntime(405): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 

更新圖片POJO:

public class Image implements Serializable { 

private static final long serialVersionUID = -2428562977284114465L; 
private Object image; 

public class ImageInner { 

    public static final String SIZE_ORIGINAL = "original"; 
    public static final String SIZE_MID = "mid"; 
    public static final String SIZE_COVER = "cover"; 
    public static final String SIZE_THUMB = "thumb"; 
    public static final String TYPE_PROFILE = "profile"; 
    public static final String TYPE_POSTER = "poster"; 
    public String type; 
    public String url; 
    public String size; 
    public int width; 
    public int height; 

} 
} 

謝謝

回答

1

你的代碼實際上是真的密切,有兩個錯誤:

1)您收到是告訴你,在JSON的一開始就期待一個數組開始時的錯誤([ ),但取而代之的是一個對象的開始({)。你發佈的JSON是而不是你正在給Gson餵食(見下文)。

2)當你糾正上述情況,你有你的Person POJO一個小問題:

public ArrayList<Profile> profile; 

應該是:

public ArrayList<Image> profile; 

你其實並不需要你Profile類; profile中的JSON 的數組。 AS-是你會收到類似你現在收到什麼其他錯誤,因爲它會被期待Profile對象的ArrayList:"profile":[{"image":[{imageobject},...]},{"image":[{imageobject},...]}]

如果固定兩個號碼和飼料GSON您已發佈的JSON,它完美的作品。你已經發布了一個Person對象的數組,但是實際上代碼中的內容(在json中)只是一個對象...或包含已發佈數組的對象。下面的小例子作品完美(矯正你的Person課後):

public class App 
{ 
     static String json = "[{\"score\":1,\"popularity\":3,\"name\":\"Brad Pitt\",\"id\":287," + 
     "\"biography\":\"test\",\"url\":\"http://www.themoviedb.org/person/287\",\"profile\":" + 
     "[{\"image\":{\"type\":\"profile\",\"size\":\"thumb\",\"height\":68,\"width\":45,\"url\":" + 
     "\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," + 
     "\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"profile\"," + 
     "\"height\":281,\"width\":185,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," + 
     "\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"h632\",\"height\":632,\"width\":416,"+ 
     "\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," + 
     "\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"original\",\"height\":1969," + 
     "\"width\":1295,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," + 
     "\"id\":\"4ea5cb8c2c0588394800006f\"}}],\"version\":685,\"last_modified_at\":\"2013-02-16 07:11:15 UTC\"" + 
     "}]"; 

    public static void main(String[] args) 
    { 

     Gson gson = new Gson(); 
     Type collectionType = new TypeToken<List<Person>>(){}.getType(); 
     List<Person> details = gson.fromJson(json, collectionType); 
    } 
} 

編輯補充:你確實有某種類型的轉換將會對GSON正在處理你...但你可能不是真的想。例如,在您的JSON scoreint,但在您的POJO中,您已將其設置爲String。 Gson默默地進行轉換。

此外,只需創建一個SSCCE(http://sscce.org/)就像我以上所做的那樣可以幫助您調試這些問題。

+0

嗨布萊恩,我會檢查收到的JSON並更新人POJO如你所述。當我嘗試時,我會提供信息。謝謝 – 2013-02-18 12:26:51

+0

謝謝你現在完美。 – 2013-02-18 20:59:50

+0

嗨布萊恩,我想我錯過了這個答案中的一點,因爲即使圖像屬性填充,當我得到人的列表觀察到所有的圖像屬性爲空。你有什麼想法嗎? – 2013-02-20 07:38:33