我正在使用jquery循环遍历json对象...但有些它似乎不起作用...
这是我的代码,
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
// data: "{}",
data: "{'currentPage':1,'pagesize':5}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(jsonObj) {
alert(jsonObj);
for (var i = jsonObj.length - 1; i >= 0; i--) {
var employee = jsonObj[i];
alert(employee.Emp_Name);
Run Code Online (Sandbox Code Playgroud)
当我jsonObj收到[object Object]警报时,但当我发出警报jsonObj.length时显示undefined任何建议....
编辑:
使用下面的答案我不能迭代divs,
$.each(jsonObj, function(i, employee) {
$('<div class="resultsdiv"><br /><span class="resultName">' + employee[i].Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category :</span> <span class="resultfieldvalues">' + employee[i].Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis :</span> <span class="resultfieldvalues">' + employee[i].SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary :</span> <span class="resultfieldvalues">' + employee[i].FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address :</span> <span class="resultfieldvalues">' + employee[i].Address + '</span></div>').insertAfter('#ResultsDiv');
});
Run Code Online (Sandbox Code Playgroud)
我的json对象就是这个,
{"Table" : [{"Row" : "1","Emp_Id" : "3","Emp_Name" : "Jerome","Address" : "Madurai","Desig_Name" : "Supervisior","SalaryBasis" : "Monthly","FixedSalary" : "25000.00"},{"Row" : "2","Emp_Id" : "4","Emp_Name" : "Mohan","Address" : "Madurai","Desig_Name" : "Acc ","SalaryBasis" : "Monthly","FixedSalary" : "200.00"},{"Row" : "3","Emp_Id" : "5","Emp_Name" : "Murugan","Address" : "Madurai","Desig_Name" : "Mason","SalaryBasis" : "Weekly","FixedSalary" : "150.00"},{"Row" : "4","Emp_Id" : "6","Emp_Name" : "Ram","Address" : "Madurai","Desig_Name" : "Mason","SalaryBasis" : "Weekly","FixedSalary" : "120.00"},{"Row" : "5","Emp_Id" : "7","Emp_Name" : "Raja","Address" : "Madurai","Desig_Name" : "Mason","SalaryBasis" : "Weekly","FixedSalary" : "135.00"}]}
我通过json选项卡中的firebug进行了检查
{"Table" : [{"Row" : "1...edSalary" : "135.00"}]}...
Response 我得到了
{"d":"{\"Table\" : [{\"Row\" : \"1\",\"Emp_Id\" : \"3\",\"Emp_Name\" : \"Jerome\",\"Address\" : \"Madurai\",\"Desig_Name\" : \"Supervisior\",\"SalaryBasis\" : \"Monthly\",\"FixedSalary\" : \"25000.00\"},{\"Row\" : \"2\",\"Emp_Id\" : \"4\",\"Emp_Name\" : \"Mohan\",\"Address\" : \"Madurai\",\"Desig_Name\" : \"Acc \",\"SalaryBasis\" : \"Monthly\",\"FixedSalary\" : \"200.00\"},{\"Row\" : \"3\",\"Emp_Id\" : \"5\",\"Emp_Name\" : \"Murugan\",\"Address\" : \"Madurai\",\"Desig_Name\" : \"Mason\",\"SalaryBasis\" : \"Weekly\",\"FixedSalary\" : \"150.00\"},{\"Row\" : \"4\",\"Emp_Id\" : \"6\",\"Emp_Name\" : \"Ram\",\"Address\" : \"Madurai\",\"Desig_Name\" : \"Mason\",\"SalaryBasis\" : \"Weekly\",\"FixedSalary\" : \"120.00\"},{\"Row\" : \"5\",\"Emp_Id\" : \"7\",\"Emp_Name\" : \"Raja\",\"Address\" : \"Madurai\",\"Desig_Name\" : \"Mason\",\"SalaryBasis\" : \"Weekly\",\"FixedSalary\" : \"135.00\"}]}"}
Run Code Online (Sandbox Code Playgroud)
任何建议......
使用jQuery的each方法.文件.
success: function(jsonObj) {
$.each(jsonObj, function(i, employee) {
alert(employee.Emp_Name);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,AFAIK alert()本身不会显示JSON结构.
因为它是一个对象(不是数组),我认为它不会有长度属性.
另外,是否有任何理由你更喜欢倒计时为了循环计数?这是一个优化技巧?
看了一些JSON示例,即
{
Table: [
{
Row: '1',
Emp_Id: '3',
Emp_Name: 'Jerome',
Address: 'Madurai',
Desig_Name: 'Supervisior',
SalaryBasis: 'Monthly',
FixedSalary: '25000.00'
},
{
Row: '2',
Emp_Id: '4',
Emp_Name: 'Mohan',
Address: 'Madurai',
Desig_Name: 'Acc ',
SalaryBasis: 'Monthly',
FixedSalary: '200.00'
},
{
Row: '3',
Emp_Id: '5',
Emp_Name: 'Murugan',
Address: 'Madurai',
Desig_Name: 'Mason',
SalaryBasis: 'Weekly',
FixedSalary: '150.00'
},
{
Row: '4',
Emp_Id: '6',
Emp_Name: 'Ram',
Address: 'Madurai',
Desig_Name: 'Mason',
SalaryBasis: 'Weekly',
FixedSalary: '120.00'
},
{
Row: '5',
Emp_Id: '7',
Emp_Name: 'Raja',
Address: 'Madurai',
Desig_Name: 'Mason',
SalaryBasis: 'Weekly',
FixedSalary: '135.00'
}
]
}
Run Code Online (Sandbox Code Playgroud)
看起来你会想要像这样访问员工的名字
$.each(jsonObj.table, function(i, employee) {
alert(employee.Emp_Name);
}
Run Code Online (Sandbox Code Playgroud)