简体   繁体   English

如何处理 JSON 数组与响应是两个对象的孩子,但不同的内容

[英]How to treat JSON Array with response are two objects childrens, but different content

Good night for everything, Hi have one problem with my app based in reddit API, where I call post detail route and the response are one array of data objects, but the primary children refers the post.大家晚安,嗨,我的基于 reddit API 的应用程序有一个问题,我在其中调用帖子详细信息路由,响应是一组数据对象,但主要孩子指的是帖子。 and the second refers to your comments.第二个是您的评论。

the complete response:完整的回应:

[
{
    "kind": "Listing",
    "data": {
        "modhash": "",
        "dist": 1,
        "children": [],
        "after": null,
        "before": null
    }
},
{
    "kind": "Listing",
    "data": {
        "modhash": "",
        "dist": null,
        "children": [],
        "after": null,
        "before": null
    }   
}
]

Where the first children is an array de data based in the post, ainda the second children is an array de data based in the post comments.第一个孩子是基于帖子的数组数据,第二个孩子是基于帖子评论的数组数据。 Both have different fields, that is, in the first children it does not have all the fields of the second children for example两者都有不同的领域,也就是说,在第一个孩子中,它没有第二个孩子的所有领域,例如

what is the best way to work with this type of result, be it in kotlin or java?处理这种结果的最佳方法是什么,无论是在 kotlin 还是 java 中?

the url example https://www.reddit.com/r/funny/comments/mei33b/updated_my_wall_art_to_be_more_relevant.json url 示例https://www.reddit.com/r/funny/comments/mei33b/updated_my_wall_art_to_be_more_relevant.Z466DEEC76ECDF5FCA6D345

Can't you just de-serialize it, below seems to work fine (i haven't bothered adding all fields)?你不能只是反序列化它,下面似乎工作正常(我没有费心添加所有字段)?

public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    try (FileInputStream fs = new FileInputStream("reddit.json")) {
        Reddit[] reddit = mapper.readValue(fs, Reddit[].class);
        System.out.println(reddit[0].kind);
        System.out.println(reddit[0].data.children[0].data.thumbnail);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static class Reddit {

    public final String kind;
    public final Data data;

    @JsonCreator
    public Reddit(
        @JsonProperty("kind") String kind,
        @JsonProperty("data") Data data
    ) {
        this.kind = kind;
        this.data = data;
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Data {

    public final String modhash;
    public final Integer dist;
    public final ChildData[] children;
    public final String after;
    public final String before;

    @JsonCreator
    public Data(
        @JsonProperty("modhash") String modhash,
        @JsonProperty("dist") Integer dist,
        @JsonProperty("children") ChildData[] children,
        @JsonProperty("after") String after,
        @JsonProperty("before") String before
    ) {
        this.modhash = modhash;
        this.dist = dist;
        this.children = children;
        this.after = after;
        this.before = before;
    }
}

public static class ChildData {

    public final String kind;
    public final ChildDataData data;

    @JsonCreator
    public ChildData(
        @JsonProperty("kind") String kind,
        @JsonProperty("data") ChildDataData data
    ) {
        this.kind = kind;
        this.data = data;
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class ChildDataData {

    public final Integer total_awards_received;
    public final String body;
    public final String thumbnail;

    @JsonCreator
    public ChildDataData(
        @JsonProperty("total_awards_received") Integer total_awards_received,
        @JsonProperty("body") String body,
        @JsonProperty("thumbnail") String thumbnail
    ) {
        this.total_awards_received = total_awards_received;
        this.body = body;
        this.thumbnail = thumbnail;
    }
}

Something like that, you probably want to change names to something that makes sense to you and you need to add all fields to ChildDataData but i think you get the point?类似的事情,您可能想将名称更改为对您有意义的名称,并且您需要将所有字段添加到 ChildDataData 但我认为您明白了吗?

Gradle dependencies Gradle 依赖项

dependencies {
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.2'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.12.2'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM