Take a look at the great God's article:
Using Gson to parse complex JSON data
Build dependency first
implementation'com.google.code.gson:gson:2.8.0'
Then get the json data according to the api
{ "status": "1", "info": "OK", "infocode": "10000", "count": "1", "geocodes": [ { "formatted_address": "Shenzhen Convention and Exhibition Center, Futian District, Shenzhen City, Guangdong Province", "province": "Guangdong Province", "citycode": "0755", "city": "Shenzhen City", "district": "Futian District", "township": [ ], "neighborhood": { "name": [ ], "type": [ ] }, "building": { "name": [ ], "type": [ ] }, "adcode": "440304", "street": [ ], "number": [ ], "location": "114.059812,22.530777", "level": "Interest point" } ] }
This data is layer by layer. It's dazzling and not intuitive. I'll borrow it here
JSON online view viewer
After pasting JSON data, click view to expand
This is a very direct view out of the structure, now you want to take out the location. We can see that the entire JSON data contains five first level subitems, one of which is an array geocodes. There is only one element {} in the first level subitem array, with the sequence number of 0, which contains a large number of subitems. Some subitems are still arrays, but just because the location is not an array, it is equivalent to being surrounded by three layers: JSON layer, geocodes layer and 0 layer, like peeling onions Just peel it off layer by layer.
To create a javabean class:
Using the GsonFormat plug-in to complete the generation
Location.java
public class Location { /** * status : 1 * info : OK * infocode : 10000 * count : 1 * geocodes : [{"formatted_address":"530777 "," level ":" points of interest "}] */ private String status; private String info; private String infocode; private String count; private List<GeocodesBean> 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<GeocodesBean> getGeocodes() { return geocodes; } public void setGeocodes(List<GeocodesBean> geocodes) { this.geocodes = geocodes; } public static class GeocodesBean { /** * formatted_address : Shenzhen Convention and Exhibition Center, Futian District, Shenzhen City, Guangdong Province * province : Guangdong Province * citycode : 0755 * city : Shenzhen City * district : Futian District * township : [] * neighborhood : {"name":[],"type":[]} * building : {"name":[],"type":[]} * adcode : 440304 * street : [] * number : [] * location : 114.059812,22.530777 * level : Interest point */ 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; } } } }
We can see that the required location data is included in the GeocodesBean class in the location class. It is recommended to cut the GeocodesBean class directly, create a new GeocodesBean class and paste it in, and delete the static of the class header. It's better not to put the data we want in the class.
I.e. modified
Location.java
public class Location { /** * status : 1 * info : OK * infocode : 10000 * count : 1 * geocodes : [{"formatted_address":"678323 "," level ":" points of interest "}] */ private String status; private String info; private String infocode; private String count; private List<GeocodesBean> 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<GeocodesBean> getGeocodes() { return geocodes; } public void setGeocodes(List<GeocodesBean> geocodes) { this.geocodes = geocodes; } }
GeocodesBean.java
public class GeocodesBean { /** * formatted_address : Hexagon building, Pingshan District, Shenzhen City, Guangdong Province * province : Guangdong Province * citycode : 0755 * city : Shenzhen City * district : Pingshan District * township : [] * neighborhood : {"name":[],"type":[]} * building : {"name":[],"type":[]} * adcode : 440310 * street : [] * number : [] * location : 114.360910,22.678323 * level : Interest point */ 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; } } }
Then the parsed function is easy to write:
private void parseJSONWithGSON(String jsonData){ Gson gson =new Gson(); Location jdata=gson.fromJson(jsonData,Location .class);//Resolve the first layer List<GeocodesBean> beanList= jdata .getGeocodes();//getGeocodes() gets an array [], encapsulated as a list Log.e("TAG","location:"+beanList.get(0).getLocation());//The data of the first (sequence number 0) of the list contains the location }
In this way, longitude and latitude data can be printed out.