HTML
<div>
<span>age - </span>
<span id='age'></span>
</div>
<div>
<span>name - </span>
<span id='name'></span>
</div>
<div>
<span>time - 24hr: </span>
<span id='24hr'></span>
<span>, 12hr: </span>
<span id='12hr'></span>
</div>
JS
window.onload=function(){
var json = '{"age": 35,"name": "","time": {"24hr": true,"12hr": false}}';
var jObject = eval("("+json+")");
document.getElementById("age").innerHTML = jObject.age;
document.getElementById("name").innerHTML = jObject.name;
document.getElementById("24hr").innerHTML = jObject.time["24hr"];
document.getElementById("12hr").innerHTML = jObject.time["12hr"];
};
注意:我强烈建议使用 JSON 解析器而不是 eval。
示例库:
- JSON2
- JSON.parse 有限支持
window.onload = function () {
var json = '{"age": 35,"name": "","time": {"24hr": true,"12hr": false}}';
var jObject = JSON.parse(json);
document.getElementById("age").innerHTML = jObject.age;
document.getElementById("name").innerHTML = jObject.name;
document.getElementById("24hr").innerHTML = jObject.time["24hr"];
document.getElementById("12hr").innerHTML = jObject.time["12hr"];
};