32

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

なぜこれが起こるのか考えていますか?

4

2 に答える 2

58

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

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

JSON.orgから:

ここに画像の説明を入力してください

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

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

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

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

出力

{ti: 4872}

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

于 2013-03-08T14:28:41.540 に答える
-2

jsonの番号の前の0を削除します

于 2020-01-08T07:28:27.287 に答える