我有一个嵌套的JSONArray。我对gson不是很熟悉。我尝试了很多教程,但都不够实用。 编辑:
[
{
"DivisionID": "2c0e9dc1-a6a7",
"DivisionName": "Qwerty",
"SubDivision": [
{
"SubDivisionID": "70c3ac53-eec6",
"SubDivisionName": "QW123",
"Vehicle": [
{
"Nopol": "00571564",
"LastUpdate": "Oct 10 2010 10:10AM",
"LastSpeed": 0,
"LastLon": 106.82176,
"Location": "KNOWHERE"
},
{
"Nopol": "352848020936627",
"LastUpdate": "Oct10201010: 10AM",
"LastSpeed": 0,
"LastLon": 10124.228,
"Location": "KNOWHERE2"
}
]
}
]
}
]
这是我到目前为止尝试的方法。编辑:
public class Post {
@SerializedName("DivisionID")
private String divisionid;
@SerializedName("DivisionName")
private String divisionname;
@SerializedName("SubDivision")
private ArrayList<SubDivision> subdivisions;
public Post(String divisionid, String divisionname) {
this.divisionid = divisionid;
this.divisionname = divisionname;
}
// getter and setter ...
public class SubDivision {
@SerializedName("SubDivisionID")
private String subdivisionid;
@SerializedName("SubDivisionName")
private String subdivisionname;
@SerializedName("Vehicle")
private ArrayList<Vehicles> vehicles;
public SubDivision (ArrayList<Vehicles> vehicles) {
this.vehicles = vehicles;
}
// getter and setter ...
public class Vehicles {
@SerializedName("Nopol")
private String nopol;
@SerializedName("LastLon")
private String lastlon;
@SerializedName("LastUpdate")
private String lastupdate;
@SerializedName("Location")
private String location;
public Vehicles(String nopol, String lastlon, String lastupdate, String location) {
this.nopol = nopol;
this.lastlon = lastlon;
this.lastupdate = lastupdate;
this.location = location;
}
// getter and setter ...
这是我的解析方法。编辑:
Type listType = new TypeToken<ArrayList<Post>>(){}.getType();
beanPostArrayList = new GsonBuilder().create().fromJson(reader, listType);
postList=new StringBuffer();
for(Post post: beanPostArrayList){
Log.d("topic asd: ", post.getDivisionid()+"");
postList.append("\n id: "+post.getDivisionid()+
"\n divname: "+post.getDivisionname());
Type listType2 = new TypeToken<ArrayList<SubDivision>>(){}.getType();
SubdivArrayList = new GsonBuilder().create().fromJson(reader, listType2);
postList2 = new StringBuffer();
for(SubDivision subdiv: SubdivArrayList){
postList.append("\n id: "+subdiv.getSubdivisionid()+
"\n subdivname: "+subdiv.getSubdivisionname());
Type listType3 = new TypeToken<ArrayList<Vehicles>>(){}.getType();
vehicleArrayList = new GsonBuilder().create().fromJson(reader, listType3);
postList3 = new StringBuffer();
for(Vehicles vehic: vehicleArrayList){
postList.append("\n nopol: "+vehic.getNopol()+
"\n lastlon: "+vehic.getLastLon()+
"\n latupdate: "+vehic.getLastUpdate()+
"\n location: "+vehic.getLocation());
}
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
txtPostList.setText(postList);
txtSubdivList.setText(postList2);
txtVehicList.setText(postList3);
}
}.execute();
问题是我不知道如何解析这个结构。我该怎么做?