我正在使用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);
当我jsonObj
收到[object Object]
警报时,但当我发出警报jsonObj.length
时显示undefined
任何建议....
编辑:
使用下面的答案我不能迭代divs,
$.each(jsonObj, function(i, employee) { $('').insertAfter('#ResultsDiv'); });
' + employee[i].Emp_Name + 'Category : ' + employee[i].Desig_Name + '
Salary Basis : ' + employee[i].SalaryBasis + 'Salary : ' + employee[i].FixedSalary + 'Address : ' + employee[i].Address + '
我的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\"}]}"}
任何建议......
使用jQuery的each
方法.文件.
success: function(jsonObj) { $.each(jsonObj, function(i, employee) { alert(employee.Emp_Name); } }
此外,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' } ] }
看起来你会想要像这样访问员工的名字
$.each(jsonObj.table, function(i, employee) { alert(employee.Emp_Name); }