web-dev-qa-db-ja.com

JSON構文エラー: 'unexpected number'または 'JSON.parse:expected'、 'or'} 'after property value in object'

$ .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は機能しません。

これが起こる理由は何ですか?

23
gustavovelascoh

数字は意味のない0で始めることはできません。

これは無効です:"ti": 0000011410

JSON.org から:

enter image description here

ソースで修正する必要がありますが、できない場合は、JSONが常にこれに似ていると仮定します(文字列に数字はありません)おそらく正規表現で修正するかもしれません:

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

"0000011410"は8進数として解析されるため、ここで悪evalを使用することはできません。

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

出力

{ti: 4872}

これはおそらく、JSONで意味のない0で始まる数字を禁止する方が安全と考えられた理由を説明しています。

49
Denys Séguret