用GSON解析嵌套的Json

时间:2015-06-07 13:34:09

标签: java android json gson

我有一个JSONArray,里面有很多JSONObjects。

对象的样本是:

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

所有这些对象都在features数组中。

所以我这样做了:

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

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

但我无法弄清楚,如何在类中定义geometryproperties属性来解析JSONObject。

我尝试:

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 个答案:

答案 0 :(得分:5)

"geometry""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;

}

完整对象:

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

"coordinates"是一个与JSON相对应的数组