2013-03-08 8 views
13

ricevo questa risposta da una richiesta POST utilizzando $ .ajax():JSON Errore di sintassi: 'inaspettato numero' o 'JSON.parse: previsto', 'o '}' dopo il valore della proprietà nell'oggetto'

{"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": "*NETEVENT*","sensors": "0B","ti": 1047215800,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047215800,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047217510,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047217511,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047217511,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047217956,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047217957,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047217957,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047218051,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047218051,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047218052,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047218052,"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": 0000001564,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047159660,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047159660,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047159661,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047159662,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047160593,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047160593,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0D","ti": 1047160594,"to": 0}, 
    {"type": 30,"tag": " __START__","sensors": "00","ti": 1047161631,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0A","ti": 1047161631,"to": 0}, 
    {"type": 30,"tag": "*NETEVENT*","sensors": "0B","ti": 1047161632,"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"} 

per IE funziona bene, in Chrome appare "Syntax error: unexpected number" e in Firefox il messaggio è "SyntaxError: JSON.parse: expected ',' or '}' after property value in object"

in diversi parser JSON on-line e validatori il formato della risposta sembra essere OK, ma in Firefox un d chrome non funziona.

Qualche idea del motivo?

+1

Viene visualizzato un errore di convalida sul valore '0000000206' con il primo validatore JSON trovato su Google. – JJJ

+0

[questo] (http://jsonlint.com/) può esserti utile nella convalida della stringa json –

risposta

33

Un numero non può iniziare con un valore non significativo 0.

Questo non è valido: "ti": 0000011410

Da JSON.org:

enter image description here

Si dovrebbe risolvere il problema alla fonte, ma se non è possibile, assumendo il vostro JSON è sempre simile a questo (nessun numero nelle stringhe), quindi si potrebbe probabilmente risolvere con un'espressione regolare:

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

Non si può nemmeno qui utilizzare il male eval perché "0000011410" sarebbe essere analizzato come un ottale:

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

uscite

{ti: 4872} 

Questo probabilmente spiega perché si è ritenuto più sicuro di vietare i numeri che iniziano con i non significativo 0 in JSON.

+5

ben chiazzata ... – Alnitak

+1

Ottima risposta. vale la pena ricordare che i letterali ottali sono anche proibiti nella modalità strict JavaScript. Avrai il seguente errore in JavaScript 'SyntaxError: i letterali ottali non sono consentiti in modalità rigorosa –

Problemi correlati