Javi
Javi

Reputation: 1

Need help for deserializing a json format

I'm trying to get the Java class for deserializing this: API Response I'm using http://www.jsonschema2pojo.org/ for getting the java classes I need. But with this json response I get this:

public class CatalogueResponse {
 @SerializedName("0")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._0 _0;
    @SerializedName("1")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._1 _1;
    @SerializedName("2")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._2 _2;
    @SerializedName("3")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._3 _3;
    @SerializedName("4")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._4 _4;
    @SerializedName("5")
    @Expose
    private uex.asee.fjrm.multimediapp.api.pojos.catalogue._5 _5;
    ...
}

But the number of items of the json is varying, so I can't handle it that way. I'm using gson annotations by the way. Any ideas?

Upvotes: 0

Views: 60

Answers (1)

Harsh Poddar
Harsh Poddar

Reputation: 2554

Why don't you try using a Map and then serializing it. I would suggest using a TreeMap and adding numbers as keys and values as the Catalogue object. Something like:

Map<Integer, Catalogue> map = new TreeMap<>();
map.put(0, catalogue0);
map.put(1, catalogue1);
...
// Now serialize the map.

This will generate the API response that you require.

To deserialize it using Gson, try the following:

Type mapType = new TypeToken<Map<Integer, Catalogue>>(){}.getType();
Map<Integer, Catalogue> catalogueMap = (Map<Integer, Catalogue>) gson.fromJson(json, mapType);

Upvotes: 1

Related Questions