JSON.parse中允许哪些字符?

时间:2017-07-18 03:50:16

标签: javascript php google-places-api google-geocoding-api

因此,在收到我的回复后,我不断收到解析错误。是否有非法人物这样的事情?

以下是回复

[{"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"},]

这是我处理响应的函数。我肯定知道它在警报之前就已经破了,因为警报没有被触发

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

3 个答案:

答案 0 :(得分:5)

一些错误。

  1. 需要将字符串放入双引号(")中。将"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ替换为"businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ"

  2. 删除以下行,

  3. 末尾的"businessname":"Wong's Garden"},]

答案 1 :(得分:2)

JSON的键值需要引号。 JSON数据中的引号较少,最后还有一个逗号和一个回车符

这样是对的:

[{"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"}]

答案 2 :(得分:1)

您的回复是无效的json格式,原因有两个:

  • “businessID”的值需要引号。
  • 在JSON的最后一个对象之后不应该有逗号(你的替换字符串函数修复了这个)。

我建议您使用此JSON工具包: