当前位置:  开发笔记 > 编程语言 > 正文

为什么我不能使用jQuery循环我的json对象?

如何解决《为什么我不能使用jQuery循环我的json对象?》经验,为你挑选了1个好方法。

我正在使用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) {
 $('

' + employee[i].Emp_Name + 'Category : ' + employee[i].Desig_Name + '

Salary Basis : ' + employee[i].SalaryBasis + 'Salary : ' + employee[i].FixedSalary + 'Address : ' + employee[i].Address + '
').insertAfter('#ResultsDiv'); });

我的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\"}]}"}

任何建议......



1> alex..:

使用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);

             }

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有