使用转义/特殊字符解析json响应

时间:2014-04-22 07:10:01

标签: javascript json

我正在尝试解析以下json字符串:

{
    "result": {
        "status": 1,
        "output": {
            "user": {
                "username": "ritisha47307958",
                "user_id": "47307958",
                "user_key": "47307958",
                "resource_key": null,
                "city": "Nagpur",
                "state": "MH",
                "country": "in"
            },
            "max_elev": "304",
            "gain": "0",
            "route_name": "MY test route",
            "route_description": "",
            "tags": "",
            "route_type_id": null,
            "route_key": "6546921793",
            "route_id": "392438284",
            "md5_route_id": "e1861008e571b188d137c6e2bf598e6b",
            "markers": [
                {
                    "photo_uuid": "",
                    "notes": "Head <b>north</b>:21.1432404:79.08450629999993:145~Turn <b>left</b> toward <b>Abhyankar Marg</b><div style=\"font-size:0.9em\">Pass by Bhide Girls Primary School (on the right in 92&nbsp;m)</div>:21.1445357:79.08463689999996:391~Turn <b>right</b> at Deluxe Shoes onto <b>Abhyankar Marg</b><div style=\"font-size:0.9em\">Destination will be on the left</div>:21.1434892:79.08105180000007:22",
                    "ele": 305.04,
                    "lat": 21.14357,
                    "lng": 79.08086,
                    "type": "1001",
                    "order": "1",
                    "dis": 546.74,
                    "meters": 560.73200272786
                },
                {
                    "photo_uuid": "",
                    "notes": "Head <b>east</b> on <b>Abhyankar Marg/Amravati Rd</b> toward <b>AH 46/NH 6</b><div style=\"font-size:0.9em\">Continue to follow Abhyankar Marg</div><div style=\"font-size:0.9em\">Pass by Deluxe Shoes (on the left in 50&nbsp;m)</div><div style=\"font-size:0.9em\">Destination will be on the right</div>:21.1435688:79.08085970000002:310",
                    "ele": 303.32,
                    "lat": 21.14136,
                    "lng": 79.08256,
                    "type": "1001",
                    "order": "2",
                    "dis": 836.42,
                    "meters": 872.0636810323
                },
                {
                    "photo_uuid": "",
                    "notes": "Head <b>southeast</b> on <b>Abhyankar Marg</b> toward <b>S Ambazari Rd</b><div style=\"font-size:0.9em\">Pass by Bank of Maharashtra (on the right in 41&nbsp;m)</div><div style=\"font-size:0.9em\">Destination will be on the right</div>:21.141359:79.08255810000003:273",
                    "ele": 304.11,
                    "lat": 21.139,
                    "lng": 79.08324,
                    "type": "1001",
                    "order": "3",
                    "dis": 1136.47,
                    "meters": 1146.4059422702
                },
                {
                    "photo_uuid": "",
                    "notes": "Head <b>north</b> on <b>Abhyankar Marg</b><div style=\"font-size:0.9em\">Walk along the playground (on the right for 100&nbsp;m)</div>:21.1390006:79.0832365:244~Slight <b>left</b> at Bank of Maharashtra to stay on <b>Abhyankar Marg</b>:21.1411287:79.08267350000006:24~Turn <b>left</b> onto <b>S Ambazari Rd</b><div style=\"font-size:0.9em\">Pass by Salpekar Petrol Pump (on the right in 180&nbsp;m)</div><div style=\"font-size:0.9em\">Destination will be on the right</div>:21.141278:79.08251719999998:1336",
                    "ele": 306.11,
                    "lat": 21.13829,
                    "lng": 79.07016,
                    "type": "1001",
                    "order": "4",
                    "dis": 2605.91,
                    "meters": 2758.8960340976
                }
            ],
            "points": [
                {
                    "lat": 21.14324,
                    "lng": 79.08451
                },
                {
                    "lat": 21.14454,
                    "lng": 79.08464
                },
                {
                    "lat": 21.14439,
                    "lng": 79.08409
                },
                {
                    "lat": 21.14425,
                    "lng": 79.08361
                },
                {
                    "lat": 21.14406,
                    "lng": 79.08297
                },
                {
                    "lat": 21.14389,
                    "lng": 79.08245
                },
                {
                    "lat": 21.14355,
                    "lng": 79.08114
                },
                {
                    "lat": 21.14351,
                    "lng": 79.08107
                },
                {
                    "lat": 21.14349,
                    "lng": 79.08105
                },
                {
                    "lat": 21.14354,
                    "lng": 79.08097
                },
                {
                    "lat": 21.14357,
                    "lng": 79.08086
                },
                {
                    "lat": 21.14357,
                    "lng": 79.08086
                },
                {
                    "lat": 21.14354,
                    "lng": 79.08097
                },
                {
                    "lat": 21.1433,
                    "lng": 79.0813
                },
                {
                    "lat": 21.14302,
                    "lng": 79.08159
                },
                {
                    "lat": 21.14278,
                    "lng": 79.08181
                },
                {
                    "lat": 21.1424,
                    "lng": 79.08204
                },
                {
                    "lat": 21.14162,
                    "lng": 79.08242
                },
                {
                    "lat": 21.14136,
                    "lng": 79.08256
                },
                {
                    "lat": 21.14136,
                    "lng": 79.08256
                },
                {
                    "lat": 21.14123,
                    "lng": 79.08264
                },
                {
                    "lat": 21.14107,
                    "lng": 79.0827
                },
                {
                    "lat": 21.14069,
                    "lng": 79.08283
                },
                {
                    "lat": 21.14026,
                    "lng": 79.08294
                },
                {
                    "lat": 21.13905,
                    "lng": 79.08325
                },
                {
                    "lat": 21.139,
                    "lng": 79.08324
                },
                {
                    "lat": 21.139,
                    "lng": 79.08324
                },
                {
                    "lat": 21.13905,
                    "lng": 79.08325
                },
                {
                    "lat": 21.13993,
                    "lng": 79.08302
                },
                {
                    "lat": 21.14069,
                    "lng": 79.08283
                },
                {
                    "lat": 21.14113,
                    "lng": 79.08267
                },
                {
                    "lat": 21.1412,
                    "lng": 79.08255
                },
                {
                    "lat": 21.14128,
                    "lng": 79.08252
                },
                {
                    "lat": 21.14126,
                    "lng": 79.08238
                },
                {
                    "lat": 21.14125,
                    "lng": 79.08212
                },
                {
                    "lat": 21.1413,
                    "lng": 79.08045
                },
                {
                    "lat": 21.14125,
                    "lng": 79.07993
                },
                {
                    "lat": 21.1412,
                    "lng": 79.07966
                },
                {
                    "lat": 21.141,
                    "lng": 79.0789
                },
                {
                    "lat": 21.14073,
                    "lng": 79.07795
                },
                {
                    "lat": 21.14025,
                    "lng": 79.07648
                },
                {
                    "lat": 21.14004,
                    "lng": 79.07581
                },
                {
                    "lat": 21.13991,
                    "lng": 79.07524
                },
                {
                    "lat": 21.13952,
                    "lng": 79.07381
                },
                {
                    "lat": 21.13939,
                    "lng": 79.07326
                },
                {
                    "lat": 21.13878,
                    "lng": 79.07143
                },
                {
                    "lat": 21.13829,
                    "lng": 79.07016
                }
            ],
            "distance_markers": {
                "metric": {
                    "markers": [
                        {
                            "num": 1,
                            "lat": "21.140260736484",
                            "lng": "79.082939811597"
                        },
                        {
                            "num": 2,
                            "lat": "21.140278836038",
                            "lng": "79.076568310366"
                        }
                    ],
                    "interval": 1000,
                    "sinceLast": 758.89603409756
                },
                "imperial": {
                    "markers": [
                        {
                            "num": 1,
                            "lat": "21.141299286627",
                            "lng": "79.080473826667"
                        }
                    ],
                    "interval": 1609.344,
                    "sinceLast": 1149.5520340976
                }
            },
            "distance": "2758.8960340976"
        },
        "errors": []
    }
}

我已尝试JSON.parseeval(在javascript中),但因为我的回复包含html内容,例如:

<b>Abhyankar Marg<\/b><div style=\"font-size:0.9em\">

解析它会显示错误:

  

未捕获的SyntaxError:意外的标识符

当我像这样逃避反斜杠时:

<b>Abhyankar Marg<\\/b><div style=\\"font-size:0.9em\\">

它成功解析为对象数组,所以我可以将单个转义符号\转换为\\,以便我可以解析给定的json响应。

1 个答案:

答案 0 :(得分:0)

假设响应的类型为String(如您暗示的那样) - 您可以使用其replace函数(传递的正则表达式将匹配所有双正斜杠):

response = response.replace(/\/\//g, '\/');