最进在公司写项目,遇到了一个特别大的json,第一眼看上去这个json特别的复杂,有多复杂呢?json返回的数据有544行,还行吧?
要是用传统的json解析,也能够解析,但是过程略显繁琐,而且不符合java面向对象的特性,下面贴上这段json代码,估计不会泄漏公司机密吧?
先贴上去吓吓你们,当然这无关紧要,不是我们的重点,我都改过很多内容了,没耐心看完的可以直接跳过去:
{
"response":
{
"header":
{
"rspType":"0",
"rspCode":"0000",
"rspDesc":"获取成功"
},
"body":
{
"orderData":[
{
"projectId":"7",
"fromDate":"4月18日",
"fromCity":"北京",
"toCity":"北京",
"title":"110",
"fromTime":"07:18",
"toTime":"07:23",
"toDate":"4月18日",
"lisPassengers":[
{
"pName":"毛主席",
"carNo":"06车厢",
"seatNo":"027号"
}],
"orderUrl":"www.baidu.com",
"orderId":"123456789",
"orderSerialId":"123456789"
},
{
"projectId":"1",
"comeDate":"4月22日",
"hotelName":"北京酒店",
"comeTimeW":"次日5:00",
"address":"北京",
"guestName":"哈哈",
"roomName":"亚麻得",
"rooms":"1",
"totalPrice":"299.00",
"orderUrl":"ww.baidu.com",
"orderId":"0",
"orderSerialId":"123456789"
},
{
"projectId":"1",
"comeDate":"4月23日",
"hotelName":"睿士主题酒店(北京木樨园店)",
"comeTimeW":"次日5:00",
"address":"北京",
"guestName":"亚麻得",
"roomName":"商务单人间",
"rooms":"1",
"totalPrice":"456.00",
"orderUrl":"www.baidu.com",
"orderId":"0",
"orderSerialId":"123456789
},
{
"projectId":"1",
"comeDate":"4月24日",
"hotelName":"苏州汇豪国际酒店",
"comeTimeW":"次日5:00",
"address":"狮山路58号近滨河路",
"guestName":"张力啊",
"roomName":"标准房",
"rooms":"1",
"totalPrice":"243.00",
"orderUrl":"www.baidu.com",
"orderId":"0",
"orderSerialId":"123456789"
},
{
"projectId":"1",
"comeDate":"4月25日",
"hotelName":"北京东长安饭店",
"comeTimeW":"23:00",
"address":"建国门外大街甲10号近艺嘉大厦",
"guestName":"沈文静",
"roomName":"普通标准间今日特价",
"rooms":"1",
"totalPrice":"388.00",
"orderUrl":"www.baidu.com",
"orderId":"0",
"orderSerialId":"123456789"
},
{
"projectId":"4",
"title":"2015美颂户外COS盛装巡游及亲子气球跑活动",
"travelDate":"4月25日",
"rplist":[
{
"projectId":"",
"rName":"2015美颂户外COS盛装巡游及亲子气球跑活动",
"rpName":"成人单票",
"rType":"1",
"getTicketWay":"凭付款成功确认短信、姓名、预订人手机号到月亮湾广场参加活动,无需取票。",
"address":"江苏省苏州市吴中区月亮湾路月亮湾广场",
"useDay":"4月25日",
"qrCodeUrl":"",
"priceFraction":"1",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
}],
"orderUrl":"www.baidu.com",
"personNameList":[
{
"personName":"测试"
}],
"comeTimeW":"",
"orderId":"12345678",
"orderSerialId":"123456789"
},
{
"depDate":"4月28日",
"depTime":"07:25",
"arrDate":"4月28日",
"arrTime":"09:40",
"depCityName":"广州",
"depName":"广州白云机场",
"dTml":"",
"arrCityName":"广元",
"arrName":"盘龙机场",
"aTml":"",
"flightNo":"CA1421",
"projectId":"2",
"airCompany":"中国国际航空",
"gate":"",
"turntableNo":"",
"orderUrl":"www.baidu.com",
"orderId":"123456789",
"orderSerialId":"123456789"
},
{
"projectId":"3",
"sceneryName":"上海野生动物园",
"tickets":"1",
"address":"上海市浦东新区南六公路178号",
"shortNumber":"T23279543",
"travelDate":"4月29日",
"qrNumber":"23279543",
"ticketTypeName":"上海野生动物园成人票",
"amount":"120.00",
"getTicketStyle":"游玩当天凭预定时提交的二代身份证至景区自助取票机处刷银联卡付款取票【仅可刷银联卡】",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"123456789"
},
{
"projectId":"3",
"sceneryName":"苏州天池花山",
"tickets":"1",
"address":"江苏苏州市吴中区藏书镇",
"shortNumber":"T66213009",
"travelDate":"4月30日",
"qrNumber":"",
"ticketTypeName":"苏州天池花山成人票",
"amount":"48.00",
"getTicketStyle":"天池山售票处或花山售票处",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"3",
"sceneryName":"江苏大阳山国家森林公园",
"tickets":"1",
"address":"江苏省苏州市新区浒墅关经济开发区阳山环路8号",
"shortNumber":"T497515",
"travelDate":"4月30日",
"qrNumber":"497515",
"ticketTypeName":"大阳山文殊寺景区成人票",
"amount":"30.00",
"getTicketStyle":"预订成功短信至文殊寺景区售票处",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"4",
"title":"南京城市名人酒店+《爸爸去哪儿》动漫亲子嘉年华—南京站",
"travelDate":"5月2日",
"rplist":[
{
"projectId":"",
"rName":"南京城市名人酒店",
"rpName":"【46期心动套餐】豪华房",
"rType":"0",
"getTicketWay":"",
"address":"鼓楼区中山北路30号(绿地中心·紫峰购物广场西北侧260米)",
"useDay":"5月2日",
"qrCodeUrl":"",
"priceFraction":"1",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
},
{
"projectId":"",
"rName":"《爸爸去哪儿》动漫亲子嘉年华—南京站",
"rpName":"【46期专享】《爸爸去哪儿》动漫亲子嘉年华—南京站亲子票(1大1小)【(5.1- 6.1)特惠】",
"rType":"1",
"getTicketWay":"在线支付成功后,旅游支付成功订单短信至取票点兑票入园",
"address":"江苏省南京市情侣园",
"useDay":"5月2日",
"qrCodeUrl":"",
"priceFraction":"1",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
}],
"orderUrl":"www.baidu.com",
"personNameList":[
{
"personName":"测试"
},
{
"personName":"测试"
}],
"comeTimeW":"",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"4",
"title":"宜兴凯宾斯基饭店+宜兴竹海风景区+陶祖圣境",
"travelDate":"5月2日",
"rplist":[
{
"projectId":"",
"rName":"宜兴凯宾斯基饭店",
"rpName":"豪华大床房",
"rType":"0",
"getTicketWay":"宜兴竹海门票请至酒店前台取",
"address":"解放东路888号",
"useDay":"5月2日",
"qrCodeUrl":"",
"priceFraction":"1",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
},
{
"projectId":"",
"rName":"宜兴竹海风景区",
"rpName":"成人票(包含“苏南第一峰”、“寂照禅寺”、镜湖桃花水母等景点)",
"rType":"1",
"getTicketWay":"景区游客中心取票窗口(游玩当天凭预订成功短信,报订单号或手机号码换票)",
"address":"无锡宜兴市湖滏镇竹海村.",
"useDay":"5月2日",
"qrCodeUrl":"",
"priceFraction":"2",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
},
{
"projectId":"",
"rName":"陶祖圣境",
"rpName":"成人票【五一专享】",
"rType":"1",
"getTicketWay":"凭确认短信至景区售票处报姓名、手机号取票",
"address":"无锡市宜兴市湖滏镇竹海村",
"useDay":"5月2日",
"qrCodeUrl":"",
"priceFraction":"2",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
}],
"orderUrl":"www.baidu.com",
"personNameList":[
{
"personName":"jj"
},
{
"personName":"出游人1"
}],
"comeTimeW":"",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"4",
"title":"糖果亲子营儿童户外",
"travelDate":"5月3日",
"rplist":[
{
"projectId":"",
"rName":"糖果亲子营儿童户外",
"rpName":"亲子1大+1小",
"rType":"1",
"getTicketWay":"凭付款成功确认短信、姓名、预订人手机号到指定地点集合签到。",
"address":"工业园区独墅湖月亮湾建屋大厦一楼沿湖102室",
"useDay":"5月3日",
"qrCodeUrl":"",
"priceFraction":"1",
"orderUrl":"",
"orderId":"",
"orderSerialId":""
}],
"orderUrl":"www.baidu.com",
"personNameList":[
{
"personName":"机票"
},
{
"personName":"出游人1"
}],
"comeTimeW":"",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"depDate":"5月5日",
"depTime":"22:15",
"arrDate":"5月5日",
"arrTime":"23:20",
"depCityName":"上海",
"depName":"上海浦东机场",
"dTml":"T1",
"arrCityName":"合肥",
"arrName":"合肥新桥机场",
"aTml":"",
"flightNo":"MU5467",
"projectId":"2",
"airCompany":"东方航空",
"gate":"",
"turntableNo":"",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"3",
"sceneryName":"苏州天池花山",
"tickets":"1",
"address":"江苏苏州市吴中区藏书镇",
"shortNumber":"T68576800",
"travelDate":"5月9日",
"qrNumber":"",
"ticketTypeName":"苏州天池花山成人票",
"amount":"48.00",
"getTicketStyle":"天池山售票处或花山售票处",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"6",
"title":"【母亲节起航】【歌诗达邮轮-赛琳娜号】5月10日上海-济州-釜山-上海4晚5日游",
"travelDate":"5月10日",
"traveEndDate":"5月14日",
"startPort":"上海",
"personsCount":"2",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"depDate":"5月14日",
"depTime":"06:45",
"arrDate":"5月14日",
"arrTime":"08:35",
"depCityName":"广州",
"depName":"广州白云机场",
"dTml":"",
"arrCityName":"温州",
"arrName":"温州龙湾机场",
"aTml":"",
"flightNo":"AQ1021",
"projectId":"2",
"airCompany":"",
"gate":"",
"turntableNo":"",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"7",
"fromDate":"5月19日",
"fromCity":"太原",
"toCity":"太原东",
"title":"6805",
"fromTime":"17:43",
"toTime":"17:48",
"toDate":"5月19日",
"lisPassengers":[
{
"pName":"王娇",
"carNo":"07车厢",
"seatNo":"001号"
}],
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"7",
"fromDate":"5月26日",
"fromCity":"太原",
"toCity":"太原北",
"title":"6818",
"fromTime":"07:18",
"toTime":"07:32",
"toDate":"5月26日",
"lisPassengers":[
{
"pName":"梁晓明",
"carNo":"01车厢",
"seatNo":"02A号"
}],
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"3",
"sceneryName":"北京凤凰岭自然风景公园",
"tickets":"1",
"address":"北京市海淀区苏家坨镇凤凰岭路19号",
"shortNumber":"T33125992",
"travelDate":"5月29日",
"qrNumber":"",
"ticketTypeName":"【春上枝头】北京凤凰岭公园成人票",
"amount":"20.00",
"getTicketStyle":"北京凤凰岭公园游客中心(",
"orderUrl":"www.baidu.com",
"orderSerialId":"12345678"
},
{
"depDate":"5月30日",
"depTime":"19:50",
"arrDate":"5月30日",
"arrTime":"20:40",
"depCityName":"厦门",
"depName":"厦门高崎机场",
"dTml":"T3",
"arrCityName":"武夷山",
"arrName":"武夷山机场",
"aTml":"",
"flightNo":"MF8089",
"projectId":"2",
"airCompany":"厦门航空",
"gate":"",
"turntableNo":"",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"depDate":"5月30日",
"depTime":"21:05",
"arrDate":"5月31日",
"arrTime":"00:05",
"depCityName":"厦门",
"depName":"厦门高崎机场",
"dTml":"T3",
"arrCityName":"成都",
"arrName":"成都双流机场",
"aTml":"T2",
"flightNo":"MF8489",
"projectId":"2",
"airCompany":"厦门航空",
"gate":"",
"turntableNo":"",
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"7",
"fromDate":"6月6日",
"fromCity":"太原",
"toCity":"太原北",
"title":"6818",
"fromTime":"07:18",
"toTime":"07:32",
"toDate":"6月6日",
"lisPassengers":[
{
"pName":"王娇",
"carNo":"02车厢",
"seatNo":"01A座"
}],
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
},
{
"projectId":"7",
"fromDate":"6月8日",
"fromCity":"太原",
"toCity":"太原北",
"title":"6805",
"fromTime":"17:43",
"toTime":"17:56",
"toDate":"6月8日",
"lisPassengers":[
{
"pName":"梁晓明",
"carNo":"07车厢",
"seatNo":"001座"
}],
"orderUrl":"www.baidu.com",
"orderId":"12345678",
"orderSerialId":"12345678"
}]
}
}
}
卧槽,是不是很吓人,这可怎么解析呢?没关系,我们抽丝剥茧看,其实这个json很简单,就两行:下面开始上大招了:看图:
嗯,其实这个json数据里只有有一个对象叫response,那么剩下的呢?
搜噶,原来这个response里面有两个对象叫header和body,我们继续看,一层层拔掉她的衣服,重点要来了,好鸡冻,我们马上就要征服她了!
擦,哈哈,原来header里面有三个字符串啊,哟西,
既然我们已经扒干净ta了,那我们开始撸代码。
现看实体类代码结构:
先看WatchBean,最大的一个对象,里面有什么呢?(配合上面的贴图看起来更爽哦)
<pre name="code" class="java">package com.tongcheng.pad.test.domin;
public class WatchBean {
public Response response;
@Override
public String toString() {
return "Results [header=" + response + "]";
}
}
就一个Response对象,那么response对象里有什么呢?
package com.tongcheng.pad.test.domin;
//Response类
public class Response {
public Header header;
public Body body;
@Override
public String toString() {
return "Results [header=" +header + ", body="
+ body + "]";
}
}
嗯,是一个header对象和一个body对象,至此,你们应该有所明白了,返回去再看看那一大串json数据,是不是很有规律?
你们肯定猜到header里是什么了、贴上来:
package com.tongcheng.pad.test.domin;
public class Header {
public String repType;// 请求类型
public String rspType;// ":"0",
public String rspCode;// ":"0000",
public String rspDesc;// ":"获取成功"
}
把body也贴了吧
package com.tongcheng.pad.test.domin;
import java.util.List;
public class Body {
public List<OrderData> orderData;
}
哎呦,body里面有一个OrderData数组,那是什么呢?继续贴。。。。
package com.tongcheng.pad.test.domin;
import java.util.List;
public class OrderData {
public String projectId;
public String fromDate;
public String fromCity;
public String toCity;
public String title;
public String fromTime;
public String toTime;
public String toDate;
public List<LisPassengers> lisPassengers;
public String orderUrl;
public String orderId;
public String orderSerialId;
public String comeDate;
public String hotelName;
public String comeTimeW;
public String address;
public String guestName;
public String roomName;
public String totalPrice;
public String travelDate;// ;:"4月25日",
public List<RpList> rpList;
public List<PersonNameList> personNameList;
public String depDate;// :"4月28日",
public String depTime;// :"07:25",
public String arrDate;// :"4月28日",
public String arrTime;// :"09:40",
public String depCityName;// :"广州",
public String depName;// :"广州白云机场",
public String dTml;// :"",
public String arrCityName;// :"广元",
public String arrName;// :"盘龙机场",
public String aTml;// :"",
public String flightNo;// :"CA1421",
public String airCompany;// :"中国国际航空",
public String gate;// :"",
public String turntableNo;// :"",
public String sceneryName;// :"上海野生动物园",
public String tickets;// :"1",
public String shortNumber;// :"T23279543",
public String qrNumber;// :"23279543",
public String ticketTypeName;// :"上海野生动物园成人票",
public String amount;// :"120.00",
public String getTicketStyle;// :"游玩当天凭预定时提交的二代身份证至景区自助取票机处刷银联卡付款取票【仅可刷银联卡】",
}
就是她了,最神秘和珍贵的资源,你们如果注意的话,会看到代码结构里还有一个
RpList
PersonNameList
LisPassengers
这三个类是什么呢,你们自己猜。。。考验你们有没有学好这门课的机会来了。。。。。
那么代码里我们怎么实际使用呢?代码其实很简单:
Gson gson = new GsonBuilder().create();
Type type = new TypeToken<WatchBean>() {
}.getType();
WatchBean watchBean = gson.fromJson(object.toString(),
type);
Log.i("-----test-----",
"lydceshi" + watchBean.response.body.orderData.get(2).hotelName);
代码中object是你从网络上获取的json数据,执行完这段代码,神奇的事情就出现了,结果是什么呢?没错,是
睿士主题酒店(北京木樨园店)
以后我们再使用json数据,就可以利用面向对象的思维来使用了,比如我们要获得depDate ,那就
watchBean.response.body.orderData.get(2).depDate,同理,等等等等,是不是特别方便?
说到分分钟解决Gson解析,其实是我在吹牛,这个问题是困扰了我两天的,我也是不断的在查资料学习再学习,学到这里我最大的收获就是:万变不离其宗,万丈高楼平地起离不开一砖一瓦的支持,数据再复杂也是有一定的数据结构的,只要你json基础好,相信再复杂的json都不再话下,不得不佩服一下谷歌,提供的Gson工具这么牛逼。额,太牛逼。