2015-02-26 11 views
5

Ho cercato di analizzare questa stringa:JSON.parse sta dando un oggetto "non definito"

[{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":  "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}, 
{"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"}, 
{"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}] 

Utilizzando JSON.parse() su questa stringa fammi vedere "indefinito" nella console. Secondo this site, il mio json è valido. Viene da un json_encode dato da una funzione php.

Se può aiutare, l'obiettivo finale è quello di scorrere questo array json. Grazie.

[EDIT]

mi sono reso conto che il mio errore è stato in realtà un problema di portata utilizzando le funzioni letterali. Sì, a volte sono un po 'stupido. Grazie a tutti per il vostro aiuto!

+0

Assicurarsi che tutte le '" 'sono adeguatamente sfuggito quando si immette al parsing. –

+0

Dove è la stringa? Come va l'oggetto? Come stai analizzarlo? Ti manca dettagli – epascarello

+0

JSFiddle che dimostrano che quanto sopra funziona in effetti con 'JSON.parse'. http://jsfiddle.net/infiniteloops/sqwyxoo5/ –

risposta

5

Questo non è String, è un JSON valida che può essere utilizzato in JavaScript:

var jsonData = [{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":  "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}, 
{"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"}, 
{"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}]; 

for(index in jsonData) { 
    alert(JSON.stringify(jsonData[index])); 
} 
0

Se vostro avere un ritorno come questo

var json_string = "[{"0":"1","1":"29","2":"27","3":"76","4":"61","5":"46","6":"2","ZoneId":"1","ZoneX":"29","ZoneY":"27","ZoneWidth":"76","ZoneHeight":"61","ZoneImage":"46","ZonePointTo":"2"},{"0":"2","1":"382","2":"226","3":"-117","4":"98","5":"46","6":"3","ZoneId":"2","ZoneX":"382","ZoneY":"226","ZoneWidth":"-117","ZoneHeight":"98","ZoneImage":"46","ZonePointTo":"3"},{"0":"3","1":"108","2":"74","3":"363","4":"83","5":"46","6":"2","ZoneId":"3","ZoneX":"108","ZoneY":"74","ZoneWidth":"363","ZoneHeight":"83","ZoneImage":"46","ZonePointTo":"2"}]" 

quindi è possibile utilizzare il JSON.parse () funzione

e decodificare i dati jSON stringa i

e tornerà si

[{"ZoneId": "1", "0": "1", "ZoneX": "29", "1": "29", "ZoneY": "27", "2":  "27", "ZoneWidth": "76", "3": "76", "ZoneHeight": "61", "4": "61", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}, 
{"ZoneId": "2", "0": "2", "ZoneX": "382", "1": "382", "ZoneY": "226", "2": "226", "ZoneWidth": "-117", "3": "-117", "ZoneHeight": "98", "4": "98", "ZoneImage": "46", "5": "46", "ZonePointTo": "3", "6": "3"}, 
{"ZoneId": "3", "0": "3", "ZoneX": "108", "1": "108", "ZoneY": "74", "2": "74", "ZoneWidth": "363", "3": "363", "ZoneHeight": "83", "4": "83", "ZoneImage": "46", "5": "46", "ZonePointTo": "2", "6": "2"}] 

dal momento che il ritorno è già un oggetto JSON, allora non c'è bisogno di utilizzare JSON.parse();