我不知道爲什麼這不顯示任何東西。我正在嘗試獲取var txt中的信息以在html中動態生成!json對象javascript混淆
function byId (id) {
return document.getElementById(id);
}
var txt = '{"characters":[' +
'{"fullName":"John Doe","speci":"human male","occup":"Web Personality","cide":"Sleeper","descr":"blah","biog":"blarg","allia":"chaos good" },' +
'{"fullName":"Jane Doe","speci":human female",occup":"Movie Producer","cide":"Citric","descr":"bluh","biog":"blurg","allia":"neutral" },' +
'{"fullName":"Canter Doma","speci":alien male",occup":"Chef","cide":"Galv","descr":"bleh","biog":"blerg","allia":"evil" }]}';
var obj = eval ("(" + txt + ")");
byId("fname").innerHTML = obj.characters[1].fullName;
byId("spec").innerHTML = obj.characters[1].speci;
byId("occu").innerHTML = obj.characters[1].occup;
byId("cid").innerHTML = obj.characters[1].cide;
byId("desc").innerHTML = obj.characters[1].descr;
byId("bio").innerHTML = obj.characters[1].biog;
byId("alli").innerHTML = obj.characters[1].allia;
我有一個測試鏈接在這裏。 http://jsfiddle.net/KLer3/
你檢查語法錯誤?嘗試運行http://jsonlint.com/並使用'JSON.parse()' – megawac
http://jsfiddle.net/KLer3/2/我修復了語法錯誤,但它仍然不起作用 – riotgear
JavaScript字符串無法跨越多行。一定要檢查你的JS控制檯是否有錯誤。 –