我有一个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();
}
}
但我无法弄清楚,如何在类中定义geometry
和properties
属性来解析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;
}
}
答案 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相对应的数组