android 解析数据之Gson2
这篇文章主要的目的是补充上一篇文章的,这次解析的是更加复杂的数据
QIYBean(这是根据Json代码使用GsonFormat自动生成的类)很尴尬由于数据复杂所以生成的代码有点长
package com.chuanxidemo.shaoxin.mymusic; import java.util.List; /** * Created by shaoxin on 2017/3/1. */ public class QIYBean { /** * status : 1 * data : {"province":"江苏省","cross_list":[{"distance":"191.482","direction":"West","name":"联谊路--绿溪路","weight":"120","level":"45000, 45000","longitude":"121.0512567","crossid":"021H51F0090093015--021H51F009009851","width":"8, 8","latitude":"31.31579861"},{"distance":"233.802","direction":"NorthEast","name":"陆家浜南路--陆家浜北路","weight":"120","level":"45000, 45000","longitude":"121.0476761","crossid":"021H51F00900930--021H51F0090093005","width":"8, 8","latitude":"31.31397833"},{"distance":"233.802","direction":"NorthEast","name":"陆家浜南路--绿溪路","weight":"120","level":"45000, 45000","longitude":"121.0476761","crossid":"021H51F00900930--021H51F009009851","width":"8, 8","latitude":"31.31397833"}],"code":"1","tel":"0512","cityadcode":"320500","areacode":"0512","timestamp":"1488353103.29","sea_area":{"adcode":"","name":""},"pos":"在政协陆家镇工作委员会附近, 在绿溪路旁边, 靠近联谊路--绿溪路路口","road_list":[{"distance":"79","direction":"North","name":"绿溪路","level":"5","longitude":"121.05","width":"8","roadid":"021H51F009009851","latitude":"31.3149"},{"distance":"152","direction":"SouthEast","name":"教堂路","level":"5","longitude":"121.048","width":"4","roadid":"021H51F0090092871","latitude":"31.3163"},{"distance":"191","direction":"West","name":"联谊路","level":"5","longitude":"121.051","width":"8","roadid":"021H51F0090093015","latitude":"31.3158"}],"result":"true","message":"Successful.","desc":"江苏省,苏州市,昆山市","city":"苏州市","districtadcode":"320583","district":"昆山市","country":"中国","provinceadcode":"320000","version":"2.0-3.0.7059.1533","adcode":"320583","poi_list":[{"distance":"161","direction":"West","tel":"0512-57671209;0512-57879719","name":"昆山市陆家人民医院","weight":"0.0","typecode":"090100","longitude":"121.047556","address":"陆家镇镇北路21号","latitude":"31.315543","entrances":[{"latitude":"31.314870","longitude":"121.047054"}],"type":"医疗保健服务;综合医院;综合医院","poiid":"B020016BMR"},{"distance":"141","direction":"SouthWest","tel":"0512-57671003","name":"陆家镇人民政府","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020007YBU"},{"distance":"141","direction":"SouthWest","tel":"","name":"陆家镇人民代表大会","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020007YBT"},{"distance":"141","direction":"SouthWest","tel":"","name":"中共昆山市陆家镇委员会","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020008HW1"},{"distance":"140","direction":"SouthWest","tel":"","name":"政协陆家镇工作委员会","weight":"0.0","typecode":"130105","longitude":"121.048395","address":"菉溪路22号","latitude":"31.314563","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020016BUS"}]} */ private String status; private DataBean data; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { /** * province : 江苏省 * cross_list : [{"distance":"191.482","direction":"West","name":"联谊路--绿溪路","weight":"120","level":"45000, 45000","longitude":"121.0512567","crossid":"021H51F0090093015--021H51F009009851","width":"8, 8","latitude":"31.31579861"},{"distance":"233.802","direction":"NorthEast","name":"陆家浜南路--陆家浜北路","weight":"120","level":"45000, 45000","longitude":"121.0476761","crossid":"021H51F00900930--021H51F0090093005","width":"8, 8","latitude":"31.31397833"},{"distance":"233.802","direction":"NorthEast","name":"陆家浜南路--绿溪路","weight":"120","level":"45000, 45000","longitude":"121.0476761","crossid":"021H51F00900930--021H51F009009851","width":"8, 8","latitude":"31.31397833"}] * code : 1 * tel : 0512 * cityadcode : 320500 * areacode : 0512 * timestamp : 1488353103.29 * sea_area : {"adcode":"","name":""} * pos : 在政协陆家镇工作委员会附近, 在绿溪路旁边, 靠近联谊路--绿溪路路口 * road_list : [{"distance":"79","direction":"North","name":"绿溪路","level":"5","longitude":"121.05","width":"8","roadid":"021H51F009009851","latitude":"31.3149"},{"distance":"152","direction":"SouthEast","name":"教堂路","level":"5","longitude":"121.048","width":"4","roadid":"021H51F0090092871","latitude":"31.3163"},{"distance":"191","direction":"West","name":"联谊路","level":"5","longitude":"121.051","width":"8","roadid":"021H51F0090093015","latitude":"31.3158"}] * result : true * message : Successful. * desc : 江苏省,苏州市,昆山市 * city : 苏州市 * districtadcode : 320583 * district : 昆山市 * country : 中国 * provinceadcode : 320000 * version : 2.0-3.0.7059.1533 * adcode : 320583 * poi_list : [{"distance":"161","direction":"West","tel":"0512-57671209;0512-57879719","name":"昆山市陆家人民医院","weight":"0.0","typecode":"090100","longitude":"121.047556","address":"陆家镇镇北路21号","latitude":"31.315543","entrances":[{"latitude":"31.314870","longitude":"121.047054"}],"type":"医疗保健服务;综合医院;综合医院","poiid":"B020016BMR"},{"distance":"141","direction":"SouthWest","tel":"0512-57671003","name":"陆家镇人民政府","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020007YBU"},{"distance":"141","direction":"SouthWest","tel":"","name":"陆家镇人民代表大会","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020007YBT"},{"distance":"141","direction":"SouthWest","tel":"","name":"中共昆山市陆家镇委员会","weight":"0.0","typecode":"130105","longitude":"121.048398","address":"菉溪路22号","latitude":"31.314555","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020008HW1"},{"distance":"140","direction":"SouthWest","tel":"","name":"政协陆家镇工作委员会","weight":"0.0","typecode":"130105","longitude":"121.048395","address":"菉溪路22号","latitude":"31.314563","type":"政府机构及社会团体;政府机关;乡镇级政府及事业单位","poiid":"B020016BUS"}] */ private String province; private String code; private String tel; private String cityadcode; private String areacode; private String timestamp; private SeaAreaBean sea_area; private String pos; private String result; private String message; private String desc; private String city; private String districtadcode; private String district; private String country; private String provinceadcode; private String version; private String adcode; private List<CrossListBean> cross_list; private List<RoadListBean> road_list; private List<PoiListBean> poi_list; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getCityadcode() { return cityadcode; } public void setCityadcode(String cityadcode) { this.cityadcode = cityadcode; } public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } public SeaAreaBean getSea_area() { return sea_area; } public void setSea_area(SeaAreaBean sea_area) { this.sea_area = sea_area; } public String getPos() { return pos; } public void setPos(String pos) { this.pos = pos; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrictadcode() { return districtadcode; } public void setDistrictadcode(String districtadcode) { this.districtadcode = districtadcode; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getProvinceadcode() { return provinceadcode; } public void setProvinceadcode(String provinceadcode) { this.provinceadcode = provinceadcode; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getAdcode() { return adcode; } public void setAdcode(String adcode) { this.adcode = adcode; } public List<CrossListBean> getCross_list() { return cross_list; } public void setCross_list(List<CrossListBean> cross_list) { this.cross_list = cross_list; } public List<RoadListBean> getRoad_list() { return road_list; } public void setRoad_list(List<RoadListBean> road_list) { this.road_list = road_list; } public List<PoiListBean> getPoi_list() { return poi_list; } public void setPoi_list(List<PoiListBean> poi_list) { this.poi_list = poi_list; } public static class SeaAreaBean { /** * adcode : * name : */ private String adcode; private String name; public String getAdcode() { return adcode; } public void setAdcode(String adcode) { this.adcode = adcode; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static class CrossListBean { /** * distance : 191.482 * direction : West * name : 联谊路--绿溪路 * weight : 120 * level : 45000, 45000 * longitude : 121.0512567 * crossid : 021H51F0090093015--021H51F009009851 * width : 8, 8 * latitude : 31.31579861 */ private String distance; private String direction; private String name; private String weight; private String level; private String longitude; private String crossid; private String width; private String latitude; public String getDistance() { return distance; } public void setDistance(String distance) { this.distance = distance; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } public String getCrossid() { return crossid; } public void setCrossid(String crossid) { this.crossid = crossid; } public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } } public static class RoadListBean { /** * distance : 79 * direction : North * name : 绿溪路 * level : 5 * longitude : 121.05 * width : 8 * roadid : 021H51F009009851 * latitude : 31.3149 */ private String distance; private String direction; private String name; private String level; private String longitude; private String width; private String roadid; private String latitude; public String getDistance() { return distance; } public void setDistance(String distance) { this.distance = distance; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } public String getWidth() { return width; } public void setWidth(String width) { this.width = width; } public String getRoadid() { return roadid; } public void setRoadid(String roadid) { this.roadid = roadid; } public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } } public static class PoiListBean { /** * distance : 161 * direction : West * tel : 0512-57671209;0512-57879719 * name : 昆山市陆家人民医院 * weight : 0.0 * typecode : 090100 * longitude : 121.047556 * address : 陆家镇镇北路21号 * latitude : 31.315543 * entrances : [{"latitude":"31.314870","longitude":"121.047054"}] * type : 医疗保健服务;综合医院;综合医院 * poiid : B020016BMR */ private String distance; private String direction; private String tel; private String name; private String weight; private String typecode; private String longitude; private String address; private String latitude; private String type; private String poiid; private List<EntrancesBean> entrances; public String getDistance() { return distance; } public void setDistance(String distance) { this.distance = distance; } public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } public String getTypecode() { return typecode; } public void setTypecode(String typecode) { this.typecode = typecode; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPoiid() { return poiid; } public void setPoiid(String poiid) { this.poiid = poiid; } public List<EntrancesBean> getEntrances() { return entrances; } public void setEntrances(List<EntrancesBean> entrances) { this.entrances = entrances; } public static class EntrancesBean { /** * latitude : 31.314870 * longitude : 121.047054 */ private String latitude; private String longitude; public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } } } } }
GsonUtil(用来解析Json数据)
package com.chuanxidemo.shaoxin.mymusic; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * Created by shaoxin on 2017/3/1. */ public class GsonUtil { public static BufferedReader bufferedReader; public static StringBuffer stringBuffer; // 将Json数据解析成相应的映射对象 public static <T> T parseJsonWithGson(String jsonData, Class<T> type) { Gson goson = new Gson(); T result = goson.fromJson(jsonData, type); return result; } // 将Json数组解析成相应的映射对象列表 public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> cls) { Gson gson = new Gson(); List<T> result = new ArrayList<>(); result = gson.fromJson(jsonData, new TypeToken<List<T>>() { }.getType()); return result; } // 根据json数据地址获取数据 public static String getData(String jsonURL) { try { stringBuffer = new StringBuffer(); URL url = new URL(jsonURL);//json地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET");//使用get方法接收 InputStream inputStream = connection.getInputStream();//得到一个输入流 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTf-8")); String sread = null; while ((sread = bufferedReader.readLine()) != null) { stringBuffer.append(sread); stringBuffer.append("\r\n"); } // Log.i("msg", "onClick: " + stringBuffer.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } }
GetGsonData(获取其中的数据)
package com.chuanxidemo.shaoxin.mymusic; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by shaoxin on 2017/3/1. */ public class GetGsonData extends AppCompatActivity { GsonUtil gsonUtil; private TextView txt; private Button getData; QIYBean qiyBean; Handler handler; List<QIYBean.DataBean.CrossListBean> listBeen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.showdata); txt = (TextView) findViewById(R.id.txt); getData = (Button) findViewById(R.id.get_data); gsonUtil = new GsonUtil(); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); txt.setText(qiyBean.getStatus()); } }; getData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread() { @Override public void run() { super.run(); String data = gsonUtil.getData("http://ditu.amap.com/service/regeo?longitude=121.04925573429551&latitude=31.315590522490712");//加载的Json数据网址 qiyBean = gsonUtil.parseJsonWithGson(data, QIYBean.class);//先获取类对象 handler.sendEmptyMessage(0x123);//线程中不能直接对UI进行设计所以用Handler解决 Log.i("msg", "run: " + qiyBean.getStatus()); listBeen = new ArrayList<QIYBean.DataBean.CrossListBean>(); listBeen = qiyBean.getData().getCross_list();//调用类中的方法获取新的对象集合 QIYBean.DataBean.CrossListBean crossListBean = listBeen.get(0);//获取集合里的第一个对象 Log.i("msg", "run: " + crossListBean.getDirection());//用对象掉方法 } }.start(); } }); } }
showdata.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/get_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取数据"/> </LinearLayout>
以上是完整代码,记得添加Gson包和添加权限