前言
在上一遍文章中,介绍了android中解析JSON数据的两种方式,还附上了相应的例子,对于还没有接触过JSON解析的童鞋可以先看上一篇博客。但客户端与服务端的交互中,往往所涉及的JSON数据都比较复杂,不是单纯的JSONObject或者JSONArray,会包含多层嵌套。这篇文章主要目的是使用上篇提到的两种方式解析复杂的JSON数据
解析复杂的JSON数据
1 需要解析的JSON数据:
{
"response_head":{
"respmenu":"getmerchant",
"resptime":"2015-05-16",
"respinfo":{
"respcode":"0000",
"respdes":"成功"
}
},
"response_body":{
"crset":[
{
"merchantname":"三火餐厅",
"menu":[
{
"menuid":"1",
"menuname":"酸菜鱼"
},
{
"menuid":"2",
"menuname":"麻婆豆腐"
},
{
"menuid":"3",
"menuname":"酸菜鱼"
}
]
},
{
"merchantname":"粤菜餐厅",
"menu":[
{
"menuid":"1",
"menuname":"炒生菜"
},
{
"menuid":"2",
"menuname":"蒸鱼"
},
{
"menuid":"3",
"menuname":"煎豆腐"
}
]
},
{
"merchantname":"西式餐厅",
"menu":[
{
"menuid":"1",
"menuname":"牛扒"
},
{
"menuid":"2",
"menuname":"罗宋汤"
},
{
"menuid":"3",
"menuname":"三文治"
}
]
}
]
}
}
2 构建相应的java对象,用来保存解析的数据
public class Response {
private Response_head response_head;
public Response_head getResponse_head() {
return response_head;
}
public void setResponse_head(Response_head response_head) {
this.response_head = response_head;
}
public static class Response_head {
private String respmenu;
private String resptime;
private Respinfo respinfo;
public String getRespmenu() {
return respmenu;
}
public void setRespmenu(String respmenu) {
this.respmenu = respmenu;
}
public String getResptime() {
return resptime;
}
public void setResptime(String resptime) {
this.resptime = resptime;
}
public Respinfo getRespinfo() {
return respinfo;
}
public void setRespinfo(Respinfo respinfo) {
this.respinfo = respinfo;
}
public static class Respinfo {
private String respcode;
private String respdes;
public String getRespcode() {
return respcode;
}
public void setRespcode(String respcode) {
this.respcode = respcode;
}
public String getRespdes() {
return respdes;
}
public void setRespdes(String respdes) {
this.respdes = respdes;
}
}
}
private Response_body response_body;
public Response_body getResponse_body() {
return response_body;
}
public void setResponse_body(Response_body response_body) {
this.response_body = response_body;
}
public static class Response_body {
private List<Merchant> crset;
public List<Merchant> getCrset() {
return crset;
}
public void setCrset(List<Merchant> crset) {
this.crset = crset;
}
public static class Merchant {
private String merchantname;
private List<Menu> menu;
public String getMerchantname() {
return merchantname;
}
public void setMerchantname(String merchantname) {
this.merchantname = merchantname;
}
public List<Menu> getMenu() {
return menu;
}
public void setMenu(List<Menu> menu) {
this.menu = menu;
}
public static class Menu {
private String menuid;
private String menuname;
public String getMenuid() {
return menuid;
}
public void setMenuid(String menuid) {
this.menuid = menuid;
}
public String getMenuname() {
return menuname;
}
public void setMenuname(String menuname) {
this.menuname = menuname;
}
}
}
}
}
3.1 使用android内置的org.json包提供的类解析,并保存在指定的对象中
Response response = new Response();
// 解析response_head
Response_head response_head = new Response_head();
JSONObject jsonObject = new JSONObject(json);
JSONObject responseHead = jsonObject.getJSONObject("response_head");
response_head.setRespmenu(responseHead.getString("respmenu"));
response_head.setResptime(responseHead.getString("resptime"));
Respinfo respInfo = new Respinfo();
JSONObject respinfo = responseHead.getJSONObject("respinfo");
respInfo.setRespcode(respinfo.getString("respcode"));
respInfo.setRespdes(respinfo.getString("respdes"));
response_head.setRespinfo(respInfo);
response.setResponse_head(response_head);
// 解析response_body
Response_body response_body = new Response_body();
JSONObject responseBody = jsonObject.getJSONObject("response_body");
JSONArray jsonArray = responseBody.getJSONArray("crset");
List<Merchant> merchants = new ArrayList<Merchant>();
for (int i = 0; i < jsonArray.length(); i++) {
// 解析每个merchant
JSONObject item = jsonArray.getJSONObject(i);
Merchant merchant = new Merchant();
merchant.setMerchantname(item.getString("merchantname"));
// 解析每个menu
List<Menu> menus = new ArrayList<Menu>();
JSONArray array = item.getJSONArray("menu");
for (int j = 0; j < array.length(); j++) {
JSONObject menuitem = array.getJSONObject(i);
Menu menu = new Menu();
menu.setMenuid(menuitem.getString("menuid"));
menu.setMenuname(menuitem.getString("menuname"));
menus.add(menu);
}
merchant.setMenu(menus);
merchants.add(merchant);
}
response_body.setCrset(merchants);
response.setResponse_body(response_body);
3.2 使用google的开源库gson解析,并保存在指定的对象中
Gson gson = new Gson();
Type type = new TypeToken<Response>(){}.getType();
Response response = gson.fromJson(json, type);
结语
1 对比3.1和3.2的解析方式,可以发现使用gson方式解析更加的便捷,也更加的高效
2 不管那种方式,都需要分析好json数据的结构,才能成功的解析。在分析的时候需要耐心,切勿急躁。