简体   繁体   中英

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. 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?

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

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?

Gradle dependencies

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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