Gson解析复杂Json数据

看一下大神的 文章:
使用Gson解析复杂的JSON数据
首先建立依赖

    implementation'com.google.code.gson:gson:2.8.0'

然后根据api获取到的json数据

{
  "status": "1",
  "info": "OK",
  "infocode": "10000",
  "count": "1",
  "geocodes": [
    {
      "formatted_address": "广东省深圳市福田区深圳会展中心",
      "province": "广东省",
      "citycode": "0755",
      "city": "深圳市",
      "district": "福田区",
      "township": [
        
      ],
      "neighborhood": {
        "name": [
          
        ],
        "type": [
          
        ]
      },
      "building": {
        "name": [
          
        ],
        "type": [
          
        ]
      },
      "adcode": "440304",
      "street": [
        
      ],
      "number": [
        
      ],
      "location": "114.059812,22.530777",
      "level": "兴趣点"
    }
  ]
}

这数据一层套一层,看着眼花缭乱,不直观,这里借用
JSON在线视图查看器
把JSON数据贴上去后,点击视图,展开
Gson解析复杂Json数据_第1张图片
这就很直观看出来结构了,现在要取出的是location。我们看到,整个JSON数据中,含有5个一级子项,其中一个子项是一个数组geocodes,这个一级子项数组中只有一个元素{},序号为0,里面包含了一大堆子项,某些子项还是数组,但是刚好location不是数组,它就相当于被JSON层,geocodes层,0层这三个层包围,像剥洋葱那样一层一层地剥开就好啦。
建立javabean 类:
用GsonFormat插件来完成生成
Location.java

public class Location {


    /**
     * status : 1
     * info : OK
     * infocode : 10000
     * count : 1
     * geocodes : [{"formatted_address":"广东省深圳市福田区深圳会展中心","province":"广东省","citycode":"0755","city":"深圳市","district":"福田区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"440304","street":[],"number":[],"location":"114.059812,22.530777","level":"兴趣点"}]
     */

    private String status;
    private String info;
    private String infocode;
    private String count;
    private List geocodes;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getInfocode() {
        return infocode;
    }

    public void setInfocode(String infocode) {
        this.infocode = infocode;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public List getGeocodes() {
        return geocodes;
    }

    public void setGeocodes(List geocodes) {
        this.geocodes = geocodes;
    }

    public static class GeocodesBean {
        /**
         * formatted_address : 广东省深圳市福田区深圳会展中心
         * province : 广东省
         * citycode : 0755
         * city : 深圳市
         * district : 福田区
         * township : []
         * neighborhood : {"name":[],"type":[]}
         * building : {"name":[],"type":[]}
         * adcode : 440304
         * street : []
         * number : []
         * location : 114.059812,22.530777
         * level : 兴趣点
         */

        private String formatted_address;
        private String province;
        private String citycode;
        private String city;
        private String district;
        private NeighborhoodBean neighborhood;
        private BuildingBean building;
        private String adcode;
        private String location;
        private String level;
        private List township;
        private List street;
        private List number;

        public String getFormatted_address() {
            return formatted_address;
        }

        public void setFormatted_address(String formatted_address) {
            this.formatted_address = formatted_address;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCitycode() {
            return citycode;
        }

        public void setCitycode(String citycode) {
            this.citycode = citycode;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public NeighborhoodBean getNeighborhood() {
            return neighborhood;
        }

        public void setNeighborhood(NeighborhoodBean neighborhood) {
            this.neighborhood = neighborhood;
        }

        public BuildingBean getBuilding() {
            return building;
        }

        public void setBuilding(BuildingBean building) {
            this.building = building;
        }

        public String getAdcode() {
            return adcode;
        }

        public void setAdcode(String adcode) {
            this.adcode = adcode;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public List getTownship() {
            return township;
        }

        public void setTownship(List township) {
            this.township = township;
        }

        public List getStreet() {
            return street;
        }

        public void setStreet(List street) {
            this.street = street;
        }

        public List getNumber() {
            return number;
        }

        public void setNumber(List number) {
            this.number = number;
        }

        public static class NeighborhoodBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }

        public static class BuildingBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }
    }
}

我们看到,需要的location数据被包含在了Location类中的类GeocodesBean里了,建议把GeocodesBean类直接剪切掉,新建一个GeocodesBean类贴进去,把类头的static删掉。最好不要把我们要的数据放在类中类。
即修改后的
Location.java

public class Location {


    /**
     * status : 1
     * info : OK
     * infocode : 10000
     * count : 1
     * geocodes : [{"formatted_address":"广东省深圳市坪山区六角大楼","province":"广东省","citycode":"0755","city":"深圳市","district":"坪山区","township":[],"neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"adcode":"440310","street":[],"number":[],"location":"114.360910,22.678323","level":"兴趣点"}]
     */

    private String status;
    private String info;
    private String infocode;
    private String count;
    private List geocodes;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getInfocode() {
        return infocode;
    }

    public void setInfocode(String infocode) {
        this.infocode = infocode;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public List getGeocodes() {
        return geocodes;
    }

    public void setGeocodes(List geocodes) {
        this.geocodes = geocodes;
    }


}

GeocodesBean.java

public class GeocodesBean {
        /**
         * formatted_address : 广东省深圳市坪山区六角大楼
         * province : 广东省
         * citycode : 0755
         * city : 深圳市
         * district : 坪山区
         * township : []
         * neighborhood : {"name":[],"type":[]}
         * building : {"name":[],"type":[]}
         * adcode : 440310
         * street : []
         * number : []
         * location : 114.360910,22.678323
         * level : 兴趣点
         */

        private String formatted_address;
        private String province;
        private String citycode;
        private String city;
        private String district;
        private NeighborhoodBean neighborhood;
        private BuildingBean building;
        private String adcode;
        private String location;
        private String level;
        private List township;
        private List street;
        private List number;

        public String getFormatted_address() {
            return formatted_address;
        }

        public void setFormatted_address(String formatted_address) {
            this.formatted_address = formatted_address;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCitycode() {
            return citycode;
        }

        public void setCitycode(String citycode) {
            this.citycode = citycode;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public NeighborhoodBean getNeighborhood() {
            return neighborhood;
        }

        public void setNeighborhood(NeighborhoodBean neighborhood) {
            this.neighborhood = neighborhood;
        }

        public BuildingBean getBuilding() {
            return building;
        }

        public void setBuilding(BuildingBean building) {
            this.building = building;
        }

        public String getAdcode() {
            return adcode;
        }

        public void setAdcode(String adcode) {
            this.adcode = adcode;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public List getTownship() {
            return township;
        }

        public void setTownship(List township) {
            this.township = township;
        }

        public List getStreet() {
            return street;
        }

        public void setStreet(List street) {
            this.street = street;
        }

        public List getNumber() {
            return number;
        }

        public void setNumber(List number) {
            this.number = number;
        }

        public static class NeighborhoodBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }

        public static class BuildingBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }
    }

然后解析的函数就好写了:

 private void parseJSONWithGSON(String jsonData){
        Gson gson =new Gson();
        Location jdata=gson.fromJson(jsonData,Location .class);//解析掉第一层
        List beanList= jdata .getGeocodes();//getGeocodes()得到的就是一个数组[],封装为list
        Log.e("TAG","location:"+beanList.get(0).getLocation());//list的第一个(即序号0)的数据中就含有location
        }

这样就可以打印出经纬度数据了。

10月9日下午2点更新
这个解析部分在解析地址为市级别和省级别(比如深圳,香港)的时候,解析部分会发生崩溃。
查看具体地点名,城市名,省级行政区三种情况的Json数据
Gson解析复杂Json数据_第2张图片
Gson解析复杂Json数据_第3张图片
Gson解析复杂Json数据_第4张图片

发现区别只在3个参数:city,citycode,district,所以去GeocodesBean.java中把它们注释掉就好了。
更改后的GeocodesBean.java

public class GeocodesBean {
        /**
         * formatted_address : 广东省深圳市坪山区六角大楼
         * province : 广东省
         * citycode : 0755
         * city : 深圳市
         * district : 坪山区
         * township : []
         * neighborhood : {"name":[],"type":[]}
         * building : {"name":[],"type":[]}
         * adcode : 440310
         * street : []
         * number : []
         * location : 114.360910,22.678323
         * level : 兴趣点
         */

        private String formatted_address;
        private String province;
        /*private String citycode;
        private String city;
        private String district;*/ //注释掉这些就可以查询市,省级级别的地区了,比如香港,广东,深圳等
        private NeighborhoodBean neighborhood;
        private BuildingBean building;
        private String adcode;
        private String location;
        private String level;
        private List township;
        private List street;
        private List number;

        public String getFormatted_address() {
            return formatted_address;
        }

        public void setFormatted_address(String formatted_address) {
            this.formatted_address = formatted_address;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

       /* public String getCitycode() {
            return citycode;
        }

        public void setCitycode(String citycode) {
            this.citycode = citycode;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }*/

        public NeighborhoodBean getNeighborhood() {
            return neighborhood;
        }

        public void setNeighborhood(NeighborhoodBean neighborhood) {
            this.neighborhood = neighborhood;
        }

        public BuildingBean getBuilding() {
            return building;
        }

        public void setBuilding(BuildingBean building) {
            this.building = building;
        }

        public String getAdcode() {
            return adcode;
        }

        public void setAdcode(String adcode) {
            this.adcode = adcode;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public List getTownship() {
            return township;
        }

        public void setTownship(List township) {
            this.township = township;
        }

        public List getStreet() {
            return street;
        }

        public void setStreet(List street) {
            this.street = street;
        }

        public List getNumber() {
            return number;
        }

        public void setNumber(List number) {
            this.number = number;
        }

        public static class NeighborhoodBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }

        public static class BuildingBean {
            private List name;
            private List type;

            public List getName() {
                return name;
            }

            public void setName(List name) {
                this.name = name;
            }

            public List getType() {
                return type;
            }

            public void setType(List type) {
                this.type = type;
            }
        }
    }

最后搞定啦

你可能感兴趣的:(Gson解析复杂Json数据)