Parse nested Json with GSON

2.9k views Asked by At

I have a JSONArray and inside that, there are many JSONObjects.

A sample of the object is:

{
    "geometry": {
        "type": "Point",
        "coordinates": [
            11.245292261254553,
            43.77014284210037
        ]
    },
    "type": "Feature",
    "properties": {
        "nome": "Biblio",
        "type": "bibl",
        "email": "[email protected]",
        "note": "",
        "indirizzo": "ERINI",
        "numero": "19"
    },
    "id": 1
},

All these objects are inside the features array.

So I did this:

try {
        array = json.getJSONArray("features");

        for (int i = 0; i < array.length(); i++) {
            Gson gson = new Gson();    
        }
}

But I can not figure out, how I can define geometry and properties attributes in the class for parsing the JSONObject.

I try with:

public class Point {

     @SerializedName("geometry")
private Geometry geometry;
@SerializedName("id")
private String id;
@SerializedName("type")
private String type;
@SerializedName("properties")
private Properties properties;


public Geometry getGeometry() {
    return geometry;
}

public void setGeometry(Geometry geometry) {
    this.geometry = geometry;
}

public String getId() {
    return id;
}

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

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Properties getProperties() {
    return properties;
}

public void setProperties(Properties properties) {
    this.properties = properties;
}

public class Geometry {
    @SerializedName("type")
    private String type;

    @SerializedName("coordinates")
    private Coordinates coordinates;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Coordinates getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(Coordinates coordinates) {
        this.coordinates = coordinates;
    }
}

public class Coordinates {



}

public class Properties {
    @SerializedName("nome")
    private String nome;
    @SerializedName("tipo")
    private String tipo;
    @SerializedName("email")
    private String email;
    @SerializedName("note")
    private String note;
    @SerializedName("indirizzo")
    private String indirizzo;
    @SerializedName("numero")
    private String numero;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getIndirizzo() {
        return indirizzo;
    }

    public void setIndirizzo(String indirizzo) {
        this.indirizzo = indirizzo;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }
}
1

There are 1 answers

0
Anton Kovalyov On BEST ANSWER

Create models for "geometry" and "properties"

public class Geometry{

    @SerializedName("type")
    private String type;
    @SerializedName("coordinates")
    private double[] coordinates;
}

public class Properties{

    @SerializedName("name")
    private String name;
    @SerializedName("type")
    private String type;
    @SerializedName("email")
    private String email;
    @SerializedName("note")
    private String note;
    @SerializedName("ind")
    private String ind;
    @SerializedName("num")
    private String num;

}

Full object:

public class YourJsonObject{
    @SerializedName("geometry")
    private Geometry geometry;
    @SerializedName("type")
    private String type;
    @SerializedName("properties")
    private Properties properties;
    @SerializedName("id")
    private String id;
}

"coordinates" is an array accordingly to your JSON