2012-08-24 243 views
1

我試圖解析this json並從中創建一個POJO。在線文檔讓我感到困惑。我懷疑我錯誤地處理了同一類型的對象有許多不同的事件。我需要一個數組嗎?那將是什麼樣子?這裏是我的班至今:用java解析json與gson

public class StoryGSONClass { 
private Response kind; 

public static class Response { 
    private String Listing; 
    private Data data; 
} 

public static class Data { 
    private String modhash; 
    private Children children; 
    private String after; 
    private String before; 
} 

public static class Children { 
    private String childKind; 
    private ChildData childData; 
} 

public static class ChildData { 
    private String domain; 
    private String banned_by; 
    private String media_embed; 
    private String subreddit; 
    private String selftext_html; 
    private String selftext; 
    private String likes; 
    private String link_flair_text; 
    private String id; 
    private String clicked; 
    private String title; 
    private String num_comments; 
    private String score; 
    private String approved_by; 
    private String over_18; 
    private String hidden; 
    private String thumbnail; 
    private String subreddit_id; 
    private String edited; 
    private String link_flair_css_class; 
    private String author_flair_css_class; 
    private String downs; 
    private String saved; 
    private String is_self; 
    private String permalink; 
    private String name; 
    private String created; 
    private String url; 
    private String author_flair_text; 
    private String author; 
    private String created_utc; 
    private String media; 
    private String num_reports; 
    private String ups; 
} 

}

其次

Gson gson = new Gson();  
StoryGSONClass target2 = gson.fromJson(objectName, StoryGSONClass.class); 

編輯:這裏是類的最新版本:

public class Response { 
    private String kind; 
    private ResponseData data; 

public static class ResponseData { 
    private String modhash; 
    private List<ResponseChildData> children; 
    private String after; 
    private String before; 
} 

public static class ResponseChildData { 
    private String kind; 
    private ChildData data; 
} 

public static class ChildData { 
    private String domain; 
    private String banned_by; 
    private Object media_embed; 
    private String subreddit; 
    private String selftext_html; 
    private String selftext; 
    private String likes; 
    private String link_flair_text; 
    private String id; 
    private boolean clicked; 
    private String title; 
    private int num_comments; 
    private int score; 
    private String approved_by; 
    private boolean over_18; 
    private boolean hidden; 
    private String thumbnail; 
    private String subreddit_id; 
    private boolean edited; 
    private boolean link_flair_css_class; 
    private boolean author_flair_css_class; 
    private int downs; 
    private boolean saved; 
    private boolean is_self; 
    private String permalink; 
    private String name; 
    private String created; 
    private String url; 
    private String author_flair_text; 
    private String author; 
    private String created_utc; 
    private String media; 
    private String num_reports; 
    private int ups; 
} 

}

回答

0

主要的父對象應該是廁所ķ是這樣的:

public class Response { 
    private String kind; 
    private ResponseData data; 

    ... 
} 

然後ResponseData是:

public class ResponseData { 
    private String modhash; 
    private List<ResponseChildData> children; // I imagine this is where you're having 
               // some confusion. You can use a list here 
               // and you should be fine. 

    ... 
} 

而且ResponseChildData是:

public class ResponseChildData { 
    private String kind; 
    private ChildData data; // This could be a Map<String, String>, but then you'd 
          // possibly lose type information. I haven't used GSON 
          // enough to know if it would balk when it sees that some 
          // values are int and boolean and refuse to deserialize. 
          // Using a specific type here is probably better anyway 

    ... 
} 

最後,ChildData是:

public class ChildData { 
    private String domain; 
    private String banned_by; 
    ... 
    ... 
    private int ups; 

    ... 
} 

然後你可以d o:

Gson gson = new Gson();  
Response target2 = gson.fromJson(objectName, Response.class); 
+0

我已經更新了課程。當孩子返回空時,一切都很好,我可以訪問像data.modhash的東西。但是當孩子回來非空時,我得到一個「java.lang.IllegalStateException:期望一個字符串,但是BEGIN_OBJECT在......」錯誤。 – cafman

+0

這是因爲您錯誤地指定了該屬性的值應該是對象時的「String」。 Gson沒有看到一個字符串(即以'「開頭的東西),而是看到了一個對象的開始(即它看到一個'{')。 –

+0

明白了。謝謝。 – cafman