gus*_*coh 24 javascript jquery json syntax-error
我使用$ .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,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000011411,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000011411,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000011412,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047215799,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047215799,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047218051,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000002598,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047068795,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047068796,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047071223,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047071224,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047071225,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000010,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 0000000012,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0C","ti": 1047130533,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000026,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000000180,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000000206,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "09","ti": 0000000212,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "08","ti": 0000000383,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 0000001562,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 0000001563,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 0000001564,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047161632,"to": 0},
{"type": 30,"tag": " __START__","sensors": "00","ti": 1047161875,"to": 0},
{"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047161876,"to": 0}
],
"response":"ok"}
Run Code Online (Sandbox Code Playgroud)
对于IE工作正常,在Chrome中出现"Syntax error: unexpected number"并在Firefox中显示消息"SyntaxError: JSON.parse: expected ',' or '}' after property value in object"
在各种在线JSON解析器和验证器中,响应的格式似乎没问题,但在firefox和chrome中不起作用.
知道为什么会这样吗?
Den*_*ret 49
一个数字不能以一个没有意义的开头0.
这是无效的: "ti": 0000011410
来自JSON.org:

你应该在源代码修复它,但如果你不能,假设你的JSON总是类似于这个(字符串中没有数字),那么你可能用正则表达式修复它:
var obj = JSON.parse(str.replace(/ 0+(?![\. }])/g, ' '));
Run Code Online (Sandbox Code Playgroud)
你甚至不能在这里使用邪恶eval因为"0000011410"将被解析为八进制:
console.log(eval('({"ti": 0000011410})'));
Run Code Online (Sandbox Code Playgroud)
输出
{ti: 4872}
Run Code Online (Sandbox Code Playgroud)
这可能解释了为什么禁止0以JSON中的非重要性开头的数字更安全.