我有这个JSON:
{
"jsonDept": [
{
"Id": "1",
"DeptName": "aaaa "
}, {
"Id": "2",
"DeptName": "bbb "
}, {
"Id": "6",
"DeptName": "ccc "
}, {
"Id": "7",
"DeptName": "ddd "
}, {
"Id": "8",
"DeptName": "eee "
}, {
"Id": "9",
"DeptName": "fff "
}, {
"Id": "12",
"DeptName": "haha "
}, {
"Id": "13",
"DeptName": "test1 "
}, {
"Id": "14",
"DeptName": "test2 "
}, {
"Id": "10",
"DeptName": "tmd "
}, {
"Id": "11",
"DeptName": "tnnd "
}
]
}
而我的JQuery:
function loadDepartmentList() {
$.ajax({
url: "QueryDepartment.ashx",
//data: "",
type: "GET",
dataType: 'json',
success: function (data) {
// how to read json here? this is not working
$.each(data, function () {
alert("Name: " + this.DeptName);
});
}
})
}
但警报显示“未定义”,并且仅弹出一次。
我想做的是解析此json,并生成html
<ul><li>DeptName</li></ul>
并将html放入div中。我在Google上搜索了很多代码,但它们不起作用。我认为这可能是开头字符串“ jsonDept”的问题。
谁能为此json写一个工作脚本?谢谢!
最佳答案
返回的对象包含一个属性,即jsonDept
。 jsonDept
属性包含部门信息的数组。
function loadDepartmentList() {
$.ajax({
url: "QueryDepartment.ashx",
//data: "",
type: "GET",
dataType: 'json',
success: function (data) {
// how to read json here? this is not working
$.each(data.jsonDept, function () {
alert("Name: " + this.DeptName);
});
}
})
}