java中用gson解析json

a0zr77ik  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(329)

我试图解析这个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;
}

}

tct7dpnv

tct7dpnv1#

主父对象应该如下所示:

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

相关问题