我使用$ .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"}
对于 IE 正常, Chrome 出现"Syntax error: unexpected number"
, Firefox 消息为"SyntaxError: JSON.parse: expected ',' or '}' after property value in object"
在各种在线JSON解析器和验证器中,响应的格式似乎没问题,但在firefox和chrome中不起作用。
知道为什么会这样吗?
答案 0 :(得分:46)
数字不能以无意义的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
开头的数字更安全。
答案 1 :(得分:-3)
删除json中数字前面的0