javascript - What characters arent allowed in a JSON.parse? - Stack Overflow

So after receiving my response, I keep getting a parse error. Is there such thing as illegal characters

So after receiving my response, I keep getting a parse error. Is there such thing as illegal characters?

Here is the response

[{"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ,"latitude": 40.733038,"longitude":-73.6840691,"address":"1201
Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID": ChIJZfl6R5NiwokRZo7PU4NPoMY
,"latitude": 40.7329359,"longitude":-73.684513,"address":"1113 Jericho Turnpike, New Hyde Park","businessname"
:"Gino's"},{"businessID": ChIJcbpnRJNiwokRrtbOKe7HQo0,"latitude": 40.733049,"longitude":-73.684006,"address"
:"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"},]

Here is my function that handles the response. I know for sure its breaking before the alert because the alert is not being triggered

var datad = $(msg).text();
    console.log(datad);
    var resultstring = datad.replace(',]',']');
    var JsonParseData = JSON.parse(resultstring);
        alert(JsonParseData); ///BREAKING BEFORE THIS LINE

So after receiving my response, I keep getting a parse error. Is there such thing as illegal characters?

Here is the response

[{"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ,"latitude": 40.733038,"longitude":-73.6840691,"address":"1201
Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID": ChIJZfl6R5NiwokRZo7PU4NPoMY
,"latitude": 40.7329359,"longitude":-73.684513,"address":"1113 Jericho Turnpike, New Hyde Park","businessname"
:"Gino's"},{"businessID": ChIJcbpnRJNiwokRrtbOKe7HQo0,"latitude": 40.733049,"longitude":-73.684006,"address"
:"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"},]

Here is my function that handles the response. I know for sure its breaking before the alert because the alert is not being triggered

var datad = $(msg).text();
    console.log(datad);
    var resultstring = datad.replace(',]',']');
    var JsonParseData = JSON.parse(resultstring);
        alert(JsonParseData); ///BREAKING BEFORE THIS LINE
Share Improve this question edited Jul 18, 2017 at 4:21 Saumini Navaratnam 8,9005 gold badges46 silver badges74 bronze badges asked Jul 18, 2017 at 3:50 CarlitosCarlitos 4194 silver badges20 bronze badges 6
  • 2 double quote the value of businessID in the object – Mantu Nigam Commented Jul 18, 2017 at 3:54
  • 1 also there's a trailing ma – anthony sottile Commented Jul 18, 2017 at 3:57
  • @AnthonySottile Im removing the ma at line....var resultstring = datad.replace(',]',']'); – Carlitos Commented Jul 18, 2017 at 3:58
  • Java is not Javascript. Please be careful about using the right tag. I've fixed it. – ajb Commented Jul 18, 2017 at 4:02
  • 1 Looks like you have random new line chars in "address" part and missing quotation marks in variable resultstring, in "businessID" part. You can check the JSON formatting here: ietf/rfc/rfc4627.txt – vishwarajanand Commented Jul 18, 2017 at 4:03
 |  Show 1 more ment

3 Answers 3

Reset to default 5

Couple of mistakes.

  1. Need to puth the string in double quotes("). Replace "businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ with "businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ"

  2. Remove the , at the end of following line "businessname":"Wong's Garden"},]

The key values of JSON require quotation marks. You have fewer quotes in the JSON data, and at last you have one more ma and one more carriage return

like this is right:

[{"businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ","latitude":"40.733038","longitude":"-73.6840691","address":"1201 Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID":"ChIJZfl6R5NiwokRZo7PU4NPoMY","latitude":"40.7329359","longitude":"-73.684513","address":"1113 Jericho Turnpike, New Hyde Park","businessname":"Gino's"},{"businessID":"ChIJcbpnRJNiwokRrtbOKe7HQo0","latitude":"40.733049","longitude":"-73.684006","address":"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"}]

Your response is an invalid json format for 2 reasons:

  • The values of the "businessID" requires quotation marks.
  • There should not be a ma after the last object of the JSON (your replace string function fix this).

I remend you to use this JSON toolkits:

  • http://jsonviewer.stack.hu/ (This orders my json although is incorrect)
  • https://jsonformatter/ (I use this a lot, my favorite)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636789a4637444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信