使用Gson解析嵌套的JsonArray在Android中

4

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

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


复制以下程序相关内容: { "Nopol": "352848020936627", "LastUpdate": "Oct 10 2010 10:10AM", "LastSpeed": 0, "LastLon": 10124.228, "Location": "KNOWHERE2" } 并将其粘贴到 http://jsonlint.com/ 进行有效的JSON检查。 - J.K
谢谢,我已经编辑了我的 JSON。 - Dream
3个回答

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;
    }
}

谢谢和抱歉,我的问题只是打错了。我已经使用了'Post[]'和'TypeToken'两个代码,当我只使用一个jsonarray而不是嵌套的jsonarray时它可以工作。 - Dream
不需要使用TypeToken。只需使用gson.fromJson(string, Post[].class);,你就可以了! - Robert Estivill

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"
                        }
                    ]
                }
            ]
        }
    ]

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;

如果它解决了你的问题,请接受它作为你问题的答案。 - M Vignesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接