记一次奇葩的JSON解析
有些东西,用的多了,会给人一种“我掌握了”的错觉。json就是其中之一:你说你解析过无数json,错了,是你用别人的轮子解析了无数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\\\":\\\"北京市东城区东长安街\\\",\\\"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数据,让我花了一整天的时间去解析,始终不得方法,期间怀疑过自己,怀疑过轮子,还怀疑Apple。
1. 目标是获取ride
所对应的数据结构。首先,经过三层脱壳,拿到如下内容:
```
{
"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\\\":\\\"北京市东城区东长安街\\\",\\\"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. 仔细观察,发现message
对应的是一个字符串,这难不倒我,取出字符串,继续转换为json,结果如下:
```
{
"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":"北京市东城区东长安街","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
对应的不就是我们需要的数据结构吗?简直唾手可得。
然而,使用的时候却发现:哎哟卧槽,这玩意怎么各种取不到值啊?我打印,不对啊,有内容,怎么就是取不到呢?
就这样反反复复折腾了一个晚上加一个上午,睡觉的时候都在思考这个问题。
我累了,盯着屏幕发呆,突然发觉:ride
对应的这串东西怎么这么多\
啊,看得我头皮发麻。诶,不对,这玩意不是转义字符
吗?json中的key不需要转义啊,难道这是?
4. ride对应的是一个字符串,而不是一个json
后台,我操你大爷...
下面是图解的形式描述整个过程,以资留念
后台
图1我
图2操!!!
图3
网友评论