2015-06-07 171 views
2

我有一個JSONArray,裏面有很多JSONObjects。使用GSON解析嵌套的Json

對象的樣品是:

{ 
    "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 
}, 

所有這些對象都是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; 
    } 
} 

回答

3

"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