上午所有,迭代與parseJSON和長度
我試圖遍歷字符串,並與$.parseJSON
分析數據,然後將其發送到一個選擇,但由於某些原因,啓動for
什麼也沒有發生的時候,沒有錯誤或類似的東西..只是,腳本停止工作(作爲一個出口,在PHP)
的JSON:
{
"response": {
"chars": {
"0": {
"guid": "728166",
"name": "Pepito",
"race": "1",
"class": "6",
"gender": "0",
"level": "80",
"money": "1412915382",
"apoints": "0",
"hpoints": "200000",
"tkills": "1731",
"title": "0"
},
"1": {
"guid": "778879",
"name": "Chocolate",
"race": "7",
"class": "8",
"gender": "1",
"level": "88",
"money": "0",
"apoints": "0",
"hpoints": "0",
"tkills": "0",
"title": "0"
}
}
}
}
該代碼:
var json = *this is the top json*;
var parsed = $.parseJSON(json);
for (var i=0; i<parsed.length; i++){
console.log(parsed.response.chars[i].name);
}
哪部分是錯誤的?據我所知,我認爲沒有錯。
謝謝!
'console.log(parsed);'在for循環之前檢查它是否被正確解析。另外你會看到'parsed'的結構,它應該表明你的循環不起作用的原因。 – diggersworld 2013-03-23 14:16:55
我已經做了,是的,解析器的工作原理。如果我刪除'for'並使用'parsed.response.chars [0] .name'顯示第一個條目,則測試:http://jsfiddle.net/eLvqY/ – wiitohzjeh 2013-03-23 14:19:28
好吧,以及你循環錯誤的節點.. 。檢查我的答案。 – diggersworld 2013-03-23 14:23:27