Android应用之——谷歌官方Json解析工具Gson的使用
一、Gson简介
Gson(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象。也就是Java对象与json字符串间的互相转换,解析。
二、使用方法
Gson的应用主要为toJson与fromJson两个转换函数,而在使用这种对象转换之前需先创建好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。即先创建好对应的javabean,javabean中的字段与要转换的json之间要一一对应,否则会出现解析失败的情况。
将json解析成javabean对象:
javabean:
public class Person { private String name; private int age; private String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender;<pre name="code" class="java">}} json字符串:
{ "name":"zhangsan","age":20,"gender":"男"}
将此json字符串解析成java对象:
Person person = new Gson().fromJson(json, Person.class);
JSONObject jsonobject = new JSONObject(json);JSONObject对象转换为json字符串
String jsonString = json.toString();
{ "status_code": "0", "result": { "card_edate": "1451491200", "edate_remark": "2015年12月31日之前免费获取", "card_remark": "1、会员尊享多种超值特权\r\n2、会员专享预存500送50", "merchant_id": "2", "card_img": "http://www.yuelife.com/srdz_pic/baralogo.jpg", "card_id": "2" }, "status_desc": "ok" }这是服务端返回的一个json串,这里面就涉及到了对象的嵌套,json串里面的result参数也是一个对象。
JSONObject jsonObject = json.optJSONObject("result");这样就又得到了一个JSONObject对象 然后又可以通过这个JSONObject对象来得到javabean对象或者直接进行解析。
String cardNumber = jsonobject.optString("card_id");这样就可以得到cardNumber的数值。
{ "status_code": "0", "result": [ { "Rows": [ { "branch_longitude": "113.9760310000", "branch_userid": "273", "branch_addr": "深圳南山区深南大道9028号益田假日广场内L3层", "branch_status": "1", "is_default": "1", "branch_phone": "13316467909,18875900656", "branch_creattime": "0", "city": "深圳市", "branch_park": "门前有停车位", "province": "广东", "branch_guidelines": "11路公交车", "branch_updatetime": "0", "merchant_id": "2", "branch_precision": "", "branch_latitude": "22.5375870000", "branch_id": "25", "branch_name": "四海一家番禺店" } ], "shop_city": "深圳市" }, { "Rows": [ { "branch_longitude": "113.3802380000", "branch_userid": "273", "branch_addr": "广东省广州市番禺区南村镇番禺大道(原迎宾路)万博中心A2栋1-2楼", "branch_status": "1", "is_default": "0", "branch_phone": "13711112346,02038823585", "branch_creattime": "0", "city": "广州市", "branch_park": "门前有停车位", "province": "广东", "branch_guidelines": "120路公交车", "branch_updatetime": "0", "merchant_id": "2", "branch_precision": "", "branch_latitude": "23.0032640000", "branch_id": "26", "branch_name": "四海一家番禺区南村镇番禺店" } ], "shop_city": "广州市" } ], "status_desc": "ok" }上面是服务端返回的json对象,这个json稍微有点复杂,涉及到了对象的嵌套和数组的嵌套。
{ "Rows": [ { "branch_longitude": "113.9760310000", "branch_userid": "273", "branch_addr": "深圳南山区深南大道9028号益田假日广场内L3层", "branch_status": "1", "is_default": "1", "branch_phone": "13316467909,18875900656", "branch_creattime": "0", "city": "深圳市", "branch_park": "门前有停车位", "province": "广东", "branch_guidelines": "11路公交车", "branch_updatetime": "0", "merchant_id": "2", "branch_precision": "", "branch_latitude": "22.5375870000", "branch_id": "25", "branch_name": "四海一家番禺店" } ], "shop_city": "深圳市" }定义的javabean如下:
public class OrderSeatRows { private List<OrderStoreBean> Rows; private String shop_city; public List<OrderStoreBean> getRows() { return Rows; } public void setRows(List<OrderStoreBean> rows) { Rows = rows; } public String getShop_city() { return shop_city; } public void setShop_city(String shop_city) { this.shop_city = shop_city; } }因为该javabean中的rows字段也是由[]组成,也就是rows也是一个数组,因此,将rows当成一个集合,里面的{}内容,也就是下面的内容也定义为一个javabean,解析成对象
public class OrderStoreBean { private String branch_addr; private String branch_userid; private String branch_id; private String branch_name; private String province; private String city; private String branch_status; private String branch_latitude; private String branch_longitude; private String is_default; private String branch_phone; private String branch_creattime; private String branch_park; private String branch_guidelines; private String branch_updatetime; private String merchant_id; private String branch_precision; public String getBranch_addr() { return branch_addr; } public void setBranch_addr(String branch_addr) { this.branch_addr = branch_addr; } public String getBranch_userid() { return branch_userid; } public void setBranch_userid(String branch_userid) { this.branch_userid = branch_userid; } public String getBranch_id() { return branch_id; } public void setBranch_id(String branch_id) { this.branch_id = branch_id; } public String getBranch_name() { return branch_name; } public void setBranch_name(String branch_name) { this.branch_name = branch_name; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getBranch_status() { return branch_status; } public void setBranch_status(String branch_status) { this.branch_status = branch_status; } public String getBranch_latitude() { return branch_latitude; } public void setBranch_latitude(String branch_latitude) { this.branch_latitude = branch_latitude; } public String getBranch_longitude() { return branch_longitude; } public void setBranch_longitude(String branch_longitude) { this.branch_longitude = branch_longitude; } public String getIs_default() { return is_default; } public void setIs_default(String is_default) { this.is_default = is_default; } public String getBranch_phone() { return branch_phone; } public void setBranch_phone(String branch_phone) { this.branch_phone = branch_phone; } public String getBranch_creattime() { return branch_creattime; } public void setBranch_creattime(String branch_creattime) { this.branch_creattime = branch_creattime; } public String getBranch_park() { return branch_park; } public void setBranch_park(String branch_park) { this.branch_park = branch_park; } public String getBranch_guidelines() { return branch_guidelines; } public void setBranch_guidelines(String branch_guidelines) { this.branch_guidelines = branch_guidelines; } public String getBranch_updatetime() { return branch_updatetime; } public void setBranch_updatetime(String branch_updatetime) { this.branch_updatetime = branch_updatetime; } public String getMerchant_id() { return merchant_id; } public void setMerchant_id(String merchant_id) { this.merchant_id = merchant_id; } public String getBranch_precision() { return branch_precision; } public void setBranch_precision(String branch_precision) { this.branch_precision = branch_precision; } }将上面的类都定义好后,就可以开始解析了
JSONArray orderJSONArray = jo.optJSONArray("result");经过上面的解析后,就得到了一个JSONArray对象,这个对象就可以根据实际情况,继续解析。
if (null != orderJSONArray&& orderJSONArray.length() > 0) { for (int i = 0; i < orderJSONArray.length(); i++) { JSONObject cityJsonObject = orderJSONArray.getJSONObject(i); OrderSeatRows orderRow = GsonUtils.toObject(cityJsonObject.toString(),OrderSeatRows.class);便利JSONArray对象,利用getJSONObject()方法,就可以得到JSONArray中所有的JSONObject对象,得到了JSONObject对象,就可以将json解析为之前定义好的对象了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。