javascript - JSON.parse gives SyntaxError inside a for loop with array -


i have array direction renderer json's stringified.

array[1].direction = '{\"routes\":[{\"bounds\":\"\....' array[1].direction = '{\"routes\":[{\"bounds\":\"\....' . . 

this did render json's on map.

for (var x=2; x<array.length; x++) {          renderdirections(array[x].direction);     }  function renderdirections(result){       var directionrenderer = new google.maps.directionsrenderer();         mydirections = json.parse(result, function(name, value){           if(/^latlngplaceholder\(/.test(value)) {                      var match = /latlngplaceholder\(([^,]+),([^,]+)\)/.exec(value);                     return new google.maps.latlng(match[1], match[2]);                 }           else{            return value;          }         });        directionrenderer.setmap(map);       directionrenderer.setdirections(mydirections);       directionrenderer.setoptions({suppressmarkers:true,preserveviewport:true});     } 

doing gives "uncaught syntaxerror: unexpected token \" error @ json.parse line.

however, if do:

for (var x=2; x<array.length; x++) {          var examplejson = '{\"routes\":[{\"bounds\":\"\....'; //note example json logged `array[x].direction`         renderdirections(examplejson);     } 

it renders json.

the \ needed when you're creating json inside string parsed object. remove \ first example , you'll have better luck.

array[1].direction = {"routes":[{"bounds":".... 

the \ tells parser "don't treat quote terminator, character." outside quoted string, it's incorrect.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -