我使用$.ajax()从POST请求收到此响应:
{"command": 6,"log_size":50,"log":[ {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047161877,"to": 0},{"type": 30,"tag": " __START__","sensors": "00","ti": 0000011410,"sensors": "0A","ti": 0000011411,"sensors": "0B","ti": 0000011412,"ti": 1047215799,"ti": 1047215800,"ti": 1047217510,"ti": 1047217511,"ti": 1047217956,"ti": 1047217957,"ti": 1047218051,"ti": 1047218052,"ti": 0000002598,"ti": 1047068795,"ti": 1047068796,"ti": 1047071223,"ti": 1047071224,"ti": 1047071225,"ti": 0000000010,"ti": 0000000012,"sensors": "0C","ti": 1047130533,"ti": 0000000026,"ti": 0000000180,"ti": 0000000206,"sensors": "09","ti": 0000000212,"sensors": "08","ti": 0000000383,"ti": 0000001562,"ti": 0000001563,"ti": 0000001564,"ti": 1047159660,"ti": 1047159661,"ti": 1047159662,"ti": 1047160593,"ti": 1047160594,"ti": 1047161631,"ti": 1047161632,"ti": 1047161875,"ti": 1047161876,"to": 0} ],"response":"ok"}
对于IE工作正常,在Chrome中出现“语法错误:意外的数字”,在Firefox中的消息是“SyntaxError:JSON.parse:expected”,’或’}’后面的对象的属性值“
在各种在线JSON解析器和验证器中,响应的格式似乎都可以,但是在Firefox和chrome中不能正常工作.
任何想法为什么会发生这种情况?
解决方法
一个数字不能以不显着的0开始.
这是无效的:“ti”:0000011410
从JSON.org:
你应该在源代码中修复它,但是如果你不能,假设你的JSON总是类似于这个(字符串中没有数字),那么你可能会用正则表达式来修复它:
var obj = JSON.parse(str.replace(/ 0+(?![\. }])/g,' '));
你甚至不能在这里使用邪恶的eval,因为“0000011410”将被解析为八进制:
console.log(eval('({"ti": 0000011410})'));
{ti: 4872}
这可能解释了为什么在JSON中禁止以非有意义的0开头的数字更为安全.