web-dev-qa-db-fra.com

Erreur de syntaxe JSON: 'nombre inattendu' ou 'JSON.parse: attendu', 'ou'} 'après la valeur de propriété dans l'objet'

Je reçois cette réponse d'une demande POST utilisant $ .ajax ():

{"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"}

Pour IE fonctionne très bien, dans Chrome apparaît "Syntax error: unexpected number" et dans Firefox le message est "SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

Dans divers analyseurs et validateurs JSON en ligne, le format de la réponse semble correct, mais dans firefox et chrome ne fonctionne pas.

Une idée pourquoi cela se produit?

23
gustavovelascoh

Un nombre ne peut pas commencer par un 0 Non significatif.

Ceci n'est pas valide: "ti": 0000011410

De JSON.org :

enter image description here

Vous devez le corriger à la source mais si vous ne le pouvez pas, en supposant que votre JSON est toujours similaire à celui-ci (pas de nombres dans les chaînes), alors vous pourrait probablement le corriger avec une expression régulière:

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

Vous ne pouvez même pas utiliser ici le mauvais eval parce que "0000011410" Serait analysé comme un octal:

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

les sorties

{ti: 4872}

Cela explique probablement pourquoi il a été jugé plus sûr d'interdire les nombres commençant par 0 Non significatif dans JSON.

49
Denys Séguret