鉴于JSON(让我们称之为这里的信息)是节点的姓名,年龄,受的对象。 Subject是包含名称和书籍键值对的JSON数组。
在这里,您需要首先访问主题节点,这是一个数组,并迭代数组以获取每个子节点。可以在每个数组元素处再次访问键值对。
以下是迭代内容的源代码。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
var info={"student": {
"name": "Tom",
"age": 3,
"subject": [
{
"science": "scscsc",
"book": "SciBook"
},
{
"maths": "mmmm",
"book": "MathBook"
},
{
"History": "hshshs",
"book": "hisBook"
}
]
}};
var subjects=info["student"]["subject"];
//Iterate all the subejcts present in the subject Node
for(i=0;i<subjects.length;i++){
// Get the information of particular subejct
$.each(subjects[i],function(key,val){
alert(i+"> Sub[ "+key+" ]="+val);
})
}
});
</script>
</head>
<body>
</body>
</html>
但这真的不应该这样做。它比简单的for循环慢得多。 – maxedison 2012-02-29 02:30:26
@maxedison只是看着一个性能测试,老实说,并没有意识到它是多少慢......好眼眸 – charlietfl 2012-02-29 02:43:33