Gson解析複雜Json資料
阿新 • • 發佈:2018-12-14
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資料貼上去後,點選檢視,展開 這就很直觀看出來結構了,現在要取出的是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<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 : 廣東省深圳市福田區深圳會展中心 * 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<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 : 廣東省深圳市坪山區六角大樓
* 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<GeocodesBean> beanList= jdata .getGeocodes();//getGeocodes()得到的就是一個數組[],封裝為list
Log.e("TAG","location:"+beanList.get(0).getLocation());//list的第一個(即序號0)的資料中就含有location
}
這樣就可以打印出經緯度資料了。