javascript - Unable to parse JSON string error in my Phonegap project -


i'm having problems while getting json php. code in below works:

[{         "vehicleid": "1",         "status": "running",         "position": "x",         "battery": "25",         "distancetravelled": "123",         "destination": "y",         "freeseats": "2",         "speed": "32"     } ] 

but, new version of code comes (without quotes in number values) , gives parse json error , doen't shown anything.

[{          "vehicleid": 1,         "status": "running",         "position": "x",         "battery": 25,         "distancetravelled": 123,         "destination": "y",         "freeseats": 2,         "speed": 32     } ] 

i checked format @ jsonlint.com , valid. error in log in eclipse is:

05-09 21:37:25.536: e/web console(336): syntaxerror: unable parse json string @ file:///android_asset/www/mobileman.js:37      34  xmlhttp.open("get",url,false);     35  xmlhttp.send();     36  var json = xmlhttp.responsetext;      37  obj = json.parse(json); 

i tried javascript code in computer on chrome , works fine. couldn't resolve problem. answers.

well if parser throwing error means value provided parse not pure json object.

about json.parse

  • json.parse - parse string json, optionally transforming value produced parsing. if string parse not valid json, syntaxerror exception thrown.

on other hand, json.stringify

  • json.stringify - convert value json, optionally replacing values if replacer function specified, or optionally including specified properties if replacer array specified.

hence in case, firstly need convert result obtained json object , try parse it.

a = [{          "vehicleid": 1,         "status": "running",         "position": "x",         "battery": 25,         "distancetravelled": 123,         "destination": "y",         "freeseats": 2,         "speed": 32     } ];  var b = json.stringify(a); var c = json.parse(b); console.log(c) 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -