jQuery JSON 语法错误:'unexpected number' or 'JSON.parse: expected ',' or '}' after property value in object'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15296196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:45:38  来源:igfitidea点击:

JSON syntax error: 'unexpected number' or 'JSON.parse: expected ',' or '}' after property value in object'

javascriptjqueryjsonsyntax-error

提问by gustavovelascoh

I receive this response from a POST request using $.ajax():

我从使用 $.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"}

For IEworks fine, in Chromeappears "Syntax error: unexpected number"and in Firefoxthe message is "SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

对于IE工作正常,在Chrome 中出现"Syntax error: unexpected number",在Firefox中消息是"SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

In various online JSON parsers and validators the format of the response seems to be OK, but in firefox and chrome not works.

在各种在线 JSON 解析器和验证器中,响应的格式似乎没问题,但在 Firefox 和 chrome 中不起作用。

Any idea why this happens?

知道为什么会这样吗?

回答by Denys Séguret

A number can't start with a not significative 0.

数字不能以不具有意义的 开头0

This is invalid : "ti": 0000011410

这是无效的: "ti": 0000011410

From JSON.org:

来自JSON.org

enter image description here

在此处输入图片说明

You should fix it at the source but if you can't, assuming your JSON is always similar to this one(no numbers in strings), then you might probably fix it with a regex :

你应该在源头修复它,但如果你不能,假设你的 JSON 总是类似于这个(字符串中没有数字),那么你可能会用正则表达式修复它:

var obj = JSON.parse(str.replace(/ 0+(?![\. }])/g, ' '));

You can't even here use the evil evalbecause "0000011410"would be parsed as a octal :

你甚至不能在这里使用 evileval因为"0000011410"会被解析为八进制:

console.log(eval('({"ti": 0000011410})'));

outputs

产出

{ti: 4872}

This probably explains why it was considered safer to forbid numbers starting with non significative 0in JSON.

这可能解释了为什么0在 JSON 中禁止以非意义开头的数字被认为更安全。

回答by Mohammed Imthiyas

remove 0 in front of the number in the json

去掉json中数字前面的0