Android使用Gson解析嵌套的JsonArray

Android使用Gson解析嵌套的JsonArray,第1张

概述我有一个嵌套的 JSONArray.我是gson的新人.我已经尝试了很多教程,但那些没有帮助. 编辑: [{ "DivisionID": "2c0e9dc1-a6a7", "DivisionName": "Qwerty", "SubDivision": [ { "SubDivisionID": "70c3ac53-eec6", 我有一个嵌套的 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","LastLon": 10124.228,"Location": "KNowHERE2"                }            ]        }    ]}]@H_403_13@  

这就是我到目前为止的尝试方式.编辑:

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 ...@H_403_13@  

这是我解析它的方式.编辑:

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();@H_403_13@  

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

解决方法 以下应该有效:

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;    }}@H_403_13@                            	          总结       

以上是内存溢出为你收集整理的Android使用Gson解析嵌套的JsonArray全部内容,希望文章能够帮你解决Android使用Gson解析嵌套的JsonArray所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127725.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存