1

我正在尝试解析这个 json并从中创建一个 POJO。在线文档让我感到困惑。我怀疑我错误地处理了同一类型的对象有许多不同出现的事实。我需要一个数组吗?那会是什么样子?到目前为止,这是我的课程:

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;
}

}

4

1 回答 1

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;

    ...
}

然后你可以这样做:

Gson gson = new Gson();    
Response target2 = gson.fromJson(objectName, Response.class);
于 2012-08-24T16:11:05.067 回答