gpt4 book ai didi

java - 在java中使用gson解析json

转载 作者:行者123 更新时间:2023-12-01 05:10:16 27 4
gpt4 key购买 nike

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

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

}

最佳答案

主要父对象应如下所示:

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

关于java - 在java中使用gson解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12112804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com