Android使用Gson解析嵌套的JsonArray

时间:2014-12-17 11:58:45

标签: android json gson arrays

我有一个嵌套的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();

问题是我不知道如何解析这个结构。我该怎么做?

3 个答案:

答案 0 :(得分:3)

以下内容应该有效:

public class Example {

    public static void main(String[] args) {

        String s = ""; // THE JSON FROM THE NETWORK
        Gson gson = new Gson();
        Post[] posts = gson.fromJson(s, Post[].class);
        for( Post p : posts ){
            System.out.println(posts.toString() );
        }
    }

    public static class Post {

        @SerializedName("DivisionID")
        String divisionId;

        @SerializedName("DivisionName")
        String divisionName;

        @SerializedName("SubDivision")
        List<SubDivision> subDivisions;
    }

    public static class SubDivision {

        @SerializedName("SubDivisionID")
        String subDivisionId;

        @SerializedName("SubDivisionName")
        String subDivisionName;

        @SerializedName("Vehicle")
        List<Vehicle> vehicles;
    }

    public static class Vehicle {

        @SerializedName("Nopol")
        String nopol;

        @SerializedName("LastUpdate")
        String lastUpdate; // should be a date!

        @SerializedName("LastSpeed")
        String lastSpeed;

        @SerializedName("LastLon")
        Double lastLon;

        @SerializedName("Location")
        String location;
    }
}

答案 1 :(得分:0)

请先更正JSON

[
        {
            "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": "1014.228",
                            "Location": "KNOWHERE2"
                        }
                    ]
                }
            ]
        }
    ]

答案 2 :(得分:0)

首先,您需要拥有有效的JSON。使用以下链接格式化和验证您的JSON。 http://jsonformatter.curiousconcept.com/

Please use the below corrected JSON:
[
        {
            "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": "1014.228",
                            "Location": "KNOWHERE2"
                        }
                    ]
                }
            ]
        }
    ]

导入以下类来解析您的JSON响应,

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;