1

I have a below JSON document in which items is an array which will contain multiple item objects. And each item object has many fields as shown below. For simplicity purpose, I am showing only one item object in an items array but it can have more.

{
    "items": [{
            "createDate": 1234567890,
            "title": "some title",
            "bp": {
                "currencyId": "USD",
                "value": 10.0
            },
            "pof": false,
            "ku": true,
            "var": {
                "quant": 10,
                "tot": 0,
                "co": 1,
                "vbp": {
                    "cid": "USD",
                    "val": 10.0
                },
                "id": 5296164,
                "sel": {
                    "style": "Short",
                    "Size": "L",
                    "Color": "Blue"
                }
            },
            "flags": {
                "low": false,
                "rpl": false,
                "scmb": false
            }
            "shipping": {
                "shippingType": "sss"
            },
            "seller": {
                "name": "",
                "sco": 0,
                "perc": 0.0,
                "id": 101215,
                "brand": true,
                "prog": "NONE"
            },
            "dur": "DA9",
            "urls": ["world", "hello"],
            "itemId": 1234
        }
    ],
    "count": {
        "ac": 3,
        "ed": 0
    }
}

How can I parse my above JSON into a POJO using Gson? I am confused because it has lot of JSON object fields in it so not able to understand how to make a class which can hold all those things.

1

1 Answer 1

1

Use JSONschema2Pojo

example usage:

jsonschema2pojo --source ../Items.json --target java-gen -a GSON -T JSON -E

output:

Bp.java  Count.java  Flags.java  Item.java  Sel.java  Seller.java  Shipping.java  Items.java  Var.java  Vbp.java

Seller.java

public class Seller {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("sco")
    @Expose
    private Integer sco;
    @SerializedName("perc")
    @Expose
    private Double perc;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("brand")
    @Expose
    private Boolean brand;
    @SerializedName("prog")
    @Expose
    private String prog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSco() {
        return sco;
    }

    public void setSco(Integer sco) {
        this.sco = sco;
    }

    public Double getPerc() {
        return perc;
    }

    public void setPerc(Double perc) {
        this.perc = perc;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Boolean getBrand() {
        return brand;
    }

    public void setBrand(Boolean brand) {
        this.brand = brand;
    }

    public String getProg() {
        return prog;
    }

    public void setProg(String prog) {
        this.prog = prog;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

Put all of those classes into a package.

items

Then you can deserialize

String line = "{}" // Read JSON in.
Gson gson = new GsonBuilder().create();
Items items = gson.fromJson(line, Items.class);
Sign up to request clarification or add additional context in comments.

2 Comments

My items is an array which will contain multiple item object? will this still work?
Yeah you should feed in your entire schema/object to this tool though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.