2014-04-22 56 views
0

我試圖解析以下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\"> 

解析它顯示引發錯誤:

Uncaught SyntaxError: Unexpected identifier

當我逃脫反斜槓像這樣:

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

它解析成對象數組成功,所以可以如何我可以單逃逸charector \轉換成\\所以可以解析該給定JSON響應。

+4

發佈您的代碼,請。 –

+0

你的JSON響應看起來不正確 - 它應該返回正確標記的數據。 –

+0

你從哪裏得到你的JSON?在獲得它之前你有機會逃脫它嗎? – phuzi

回答

0

假設響應String型的(如你暗示) - 您可以使用它的replace功能(通過正則表達式匹配所有的雙正向斜槓):

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