記一次奇葩的JSON解析
有些東西雌团,用的多了,會給人一種“我掌握了”的錯覺阁猜。json就是其中之一:你說你解析過無數(shù)json,錯了蹋艺,是你用別人的輪子解析了無數(shù)json剃袍,你熟悉的是api,而非json捎谨。
{
"data" : [
{
"syncKey" : 53,
"message" : {
"msg_type" : 1,
"timestamp" : 1512795235374,
"message" : "{\"ride\":\"{\\\"aggregate_start_time\\\":\\\"20171209130300\\\",\\\"comment\\\":\\\"\\\",\\\"focus_arrive_first\\\":1,\\\"focus_taxi_ride\\\":{\\\"extra_fee\\\":0,\\\"from_poi\\\":{\\\"business\\\":\\\"廣順北大街\\\",\\\"city\\\":{\\\"baidu_city_id\\\":131,\\\"city_ename\\\":\\\"beijing\\\",\\\"city_name\\\":\\\"beijing\\\",\\\"id\\\":1,\\\"province_ename\\\":\\\"beijing\\\",\\\"province_name\\\":\\\"北京\\\"},\\\"latitude\\\":\\\"40.026140\\\",\\\"long_address\\\":\\\"來廣營(地鐵站)\\\",\\\"longitude\\\":\\\"116.473480\\\",\\\"short_address\\\":\\\"來廣營(地鐵站)\\\",\\\"street\\\":\\\"廣順北大街\\\"},\\\"passenger_info\\\":{\\\"avatar_url\\\":\\\"null\/20170331\/58ddcfeb30704.jpeg\\\",\\\"gender\\\":2,\\\"nick_name\\\":\\\"白云ddd\\\",\\\"phone_no\\\":\\\"16019920915\\\",\\\"score\\\":\\\"4.6\\\",\\\"user_cid\\\":\\\"984478b4-0c21-4b1c-b896-e87d6cf5ed4a\\\"},\\\"person_num\\\":1,\\\"price\\\":\\\"65.2\\\",\\\"review_closed\\\":0,\\\"status\\\":1,\\\"system_closed\\\":0,\\\"taxi_ride_id\\\":176197,\\\"to_poi\\\":{\\\"business\\\":\\\"中華路\\\",\\\"city\\\":{\\\"baidu_city_id\\\":131,\\\"city_ename\\\":\\\"beijing\\\",\\\"city_name\\\":\\\"beijing\\\",\\\"id\\\":1,\\\"province_ename\\\":\\\"beijing\\\",\\\"province_name\\\":\\\"北京\\\"},\\\"latitude\\\":\\\"39.915120\\\",\\\"long_address\\\":\\\"北京市東城區(qū)東長安街\\\",\\\"longitude\\\":\\\"116.403960\\\",\\\"short_address\\\":\\\"天安門\\\",\\\"street\\\":\\\"中華路\\\"}},\\\"pick_by_meter\\\":0,\\\"real_time\\\":1,\\\"score\\\":-25,\\\"start_distance\\\":\\\"0.3\\\",\\\"status\\\":\\\"new\\\",\\\"total_distance\\\":\\\"21.1\\\"}\"}"
}
}
],
"finish" : true,
"sid" : "7c0c14df-7591-42f7-b3d1-bd2cc5808c98",
"type" : 5,
"packageType" : 0
}
就是上面的這串json數(shù)據(jù)笛园,讓我花了一整天的時間去解析,始終不得方法侍芝,期間懷疑過自己,懷疑過輪子埋同,還懷疑Apple州叠。
1. 目標是獲取ride
所對應(yīng)的數(shù)據(jù)結(jié)構(gòu)。首先凶赁,經(jīng)過三層脫殼咧栗,拿到如下內(nèi)容:
```
{
"message" : "{\"ride\":\"{\\\"aggregate_start_time\\\":\\\"20171209130300\\\",\\\"comment\\\":\\\"\\\",\\\"focus_arrive_first\\\":1,\\\"focus_taxi_ride\\\":{\\\"extra_fee\\\":0,\\\"from_poi\\\":{\\\"business\\\":\\\"廣順北大街\\\",\\\"city\\\":{\\\"baidu_city_id\\\":131,\\\"city_ename\\\":\\\"beijing\\\",\\\"city_name\\\":\\\"beijing\\\",\\\"id\\\":1,\\\"province_ename\\\":\\\"beijing\\\",\\\"province_name\\\":\\\"北京\\\"},\\\"latitude\\\":\\\"40.026140\\\",\\\"long_address\\\":\\\"來廣營(地鐵站)\\\",\\\"longitude\\\":\\\"116.473480\\\",\\\"short_address\\\":\\\"來廣營(地鐵站)\\\",\\\"street\\\":\\\"廣順北大街\\\"},\\\"passenger_info\\\":{\\\"avatar_url\\\":\\\"null/20170331/58ddcfeb30704.jpeg\\\",\\\"gender\\\":2,\\\"nick_name\\\":\\\"白云ddd\\\",\\\"phone_no\\\":\\\"16019920915\\\",\\\"score\\\":\\\"4.6\\\",\\\"user_cid\\\":\\\"984478b4-0c21-4b1c-b896-e87d6cf5ed4a\\\"},\\\"person_num\\\":1,\\\"price\\\":\\\"65.2\\\",\\\"review_closed\\\":0,\\\"status\\\":1,\\\"system_closed\\\":0,\\\"taxi_ride_id\\\":176197,\\\"to_poi\\\":{\\\"business\\\":\\\"中華路\\\",\\\"city\\\":{\\\"baidu_city_id\\\":131,\\\"city_ename\\\":\\\"beijing\\\",\\\"city_name\\\":\\\"beijing\\\",\\\"id\\\":1,\\\"province_ename\\\":\\\"beijing\\\",\\\"province_name\\\":\\\"北京\\\"},\\\"latitude\\\":\\\"39.915120\\\",\\\"long_address\\\":\\\"北京市東城區(qū)東長安街\\\",\\\"longitude\\\":\\\"116.403960\\\",\\\"short_address\\\":\\\"天安門\\\",\\\"street\\\":\\\"中華路\\\"}},\\\"pick_by_meter\\\":0,\\\"real_time\\\":1,\\\"score\\\":-25,\\\"start_distance\\\":\\\"0.3\\\",\\\"status\\\":\\\"new\\\",\\\"total_distance\\\":\\\"21.1\\\"}\"}"
}
```
2. 仔細觀察,發(fā)現(xiàn)message
對應(yīng)的是一個字符串虱肄,這難不倒我致板,取出字符串,繼續(xù)轉(zhuǎn)換為json咏窿,結(jié)果如下:
```
{
"ride":"{"aggregate_start_time":"20171209130300","comment":"","focus_arrive_first":1,"focus_taxi_ride":{"extra_fee":0,"from_poi":{"business":"廣順北大街","city":{"baidu_city_id":131,"city_ename":"beijing","city_name":"beijing","id":1,"province_ename":"beijing","province_name":"北京"},"latitude":"40.026140","long_address":"來廣營(地鐵站)","longitude":"116.473480","short_address":"來廣營(地鐵站)","street":"廣順北大街"},"passenger_info":{"avatar_url":"null/20170331/58ddcfeb30704.jpeg","gender":2,"nick_name":"白云ddd","phone_no":"16019920915","score":"4.6","user_cid":"984478b4-0c21-4b1c-b896-e87d6cf5ed4a"},"person_num":1,"price":"65.2","review_closed":0,"status":1,"system_closed":0,"taxi_ride_id":176197,"to_poi":{"business":"中華路","city":{"baidu_city_id":131,"city_ename":"beijing","city_name":"beijing","id":1,"province_ename":"beijing","province_name":"北京"},"latitude":"39.915120","long_address":"北京市東城區(qū)東長安街","longitude":"116.403960","short_address":"天安門","street":"中華路"}},"pick_by_meter":0,"real_time":1,"score":-25,"start_distance":"0.3","status":"new","total_distance":"21.1"}"
}
```
3. 嘿嘿嘿斟或,ride
對應(yīng)的不就是我們需要的數(shù)據(jù)結(jié)構(gòu)嗎?簡直唾手可得集嵌。
然而萝挤,使用的時候卻發(fā)現(xiàn):哎喲臥槽,這玩意怎么各種取不到值案贰怜珍?我打印,不對啊凤粗,有內(nèi)容酥泛,怎么就是取不到呢?
就這樣反反復(fù)復(fù)折騰了一個晚上加一個上午嫌拣,睡覺的時候都在思考這個問題柔袁。
我累了,盯著屏幕發(fā)呆亭罪,突然發(fā)覺:ride
對應(yīng)的這串東西怎么這么多\
啊瘦馍,看得我頭皮發(fā)麻。誒应役,不對情组,這玩意不是轉(zhuǎn)義字符
嗎燥筷?json中的key不需要轉(zhuǎn)義啊,難道這是院崇?
4. ride對應(yīng)的是一個字符串肆氓,而不是一個json
后臺,我操你大爺...
下面是圖解的形式描述整個過程底瓣,以資留念
后臺
我
操P痪尽!捐凭!