Javascript TypeError:JSON数组对象未定义

时间:2013-01-11 14:34:48

标签: javascript json

我从服务器响应中获得了一个完美创建的JSON对象。

例如:

{
    "users": [
        {
            "userId": 20410,
            "firstName": "Viral",
            "lastName": "Shah",
            "loginId": "viralp.shah@tcs.com",
            "userRole": 3
        },
        {
            "userId": 400881,
            "firstName": "Viral",
            "lastName": "Shah",
            "loginId": "viralpshah123@gmail.com",
            "userRole": 0
        },
        {
            "userId": 425622,
            "firstName": "Viral",
            "lastName": "Shah",
            "loginId": "viralpshah123@tcs-itontap.com",
            "userRole": 0
        }
    ]
}

我在JavaScript中使用AJAX,如下所示:

var jsonobj2 = null;
var respObj = getSearchedWPUsers(firstname, lastname, loginid); //return json response
var len = respObj.length;
jsonobj2 = eval('(' + respObj + ')');


var tablehtml = "<table><tr><td><b>First Name</td><td><b>Last Name</td><td><b>Login Id</td><td><b>Editing Rights</td></tr><tr></tr>";

for (i = 0; i < len; i++) {
  tablehtml = tablehtml + "<tr>";
  //--------------
  tablehtml = tablehtml + "<td>";
  tablehtml = tablehtml + jsonobj2.users[i].firstName;
  tablehtml = tablehtml + "</td>";
  //--------------
  tablehtml = tablehtml + "<td>";
  tablehtml = tablehtml + jsonobj2.users[i].lastName;
  tablehtml = tablehtml + "</td>";
  //--------------
  tablehtml = tablehtml + "<td>";
  tablehtml = tablehtml + jsonobj2.users[i].loginId;
  tablehtml = tablehtml + "</td>";
  //--------------
  tablehtml = tablehtml + "<td><b>";
  var role = jsonobj2.users[i].userRole;
  if (role == 1 || role == 2 || role == 3) tablehtml = tablehtml + "<a href ='javascript:removeXML(" + jsonobj2.users[i].userId + ")'><u><font color='red'>Revoke access</font></a> ";
  else tablehtml = tablehtml + "<a href ='javascript:generateXML(" + jsonobj2.users[i].userId + ")'><u><font color='blue'>Assign access</font></a> ";

  tablehtml = tablehtml + "</td>";
  tablehtml = tablehtml + "</tr>";

}
tablehtml = tablehtml + "</table>";


document.getElementById("TableHolder").innerHTML = tablehtml;

//--------------------------

它会抛出如下错误

  

TypeError: jsonobj2.users[i] is undefined
  [Break On This Error]
  tablehtml = tablehtml + jsonobj2.users[i].firstName;

尝试JSON.parse(serverresponse); - 没有发生任何事情

请帮忙

1 个答案:

答案 0 :(得分:2)

请改为尝试:

jsonobj2 = eval('(' + respObj + ')');
var len = jsonobj2.users.length;

据我了解,respObj是一个包含JSON响应的字符串,但仍需要进行解析。因此,如果你执行respObj.length,那么正确地获取字符串的长度,而不是用户数组的长度。另外,我建议您使用JSON.parse解析此响应(只要浏览器可用),而不是使用eval

var respObj = getSearchedWPUsers(firstname, lastname, loginid);
var jsonobj2 = JSON.parse ? JSON.parse(respObj) : eval('(' + respObj + ')');
var len = respObj && respObj.users ? respObj.users.length : 0;

然而,正如Felix Kling和Aamir Adnan所说,ajax调用大多是异步的,所以你可以传递一个回调函数作为参数,它将按照你的意愿处理响应:

getSearchedWPUsers(firstname, lastname, loginid, function(respObj){
    var jsonobj2 = JSON.parse ? JSON.parse(respObj) : eval('(' + respObj + ')');
    var len = respObj && respObj.users ? respObj.users.length : 0;
    var tablehtml = "<table><tr><td><b>First Name</td><td><b>Last Name</td><td><b>Login Id</td><td><b>Editing Rights</td></tr><tr></tr>";

    for (i = 0; i < len; i++) {
        tablehtml = tablehtml + "<tr>";
        //--------------
        tablehtml = tablehtml + "<td>";
        tablehtml = tablehtml + jsonobj2.users[i].firstName;
        tablehtml = tablehtml + "</td>";
        //--------------
        tablehtml = tablehtml + "<td>";
        tablehtml = tablehtml + jsonobj2.users[i].lastName;
        tablehtml = tablehtml + "</td>";
        //--------------
        tablehtml = tablehtml + "<td>";
        tablehtml = tablehtml + jsonobj2.users[i].loginId;
        tablehtml = tablehtml + "</td>";
        //--------------
        tablehtml = tablehtml + "<td><b>";
        var role = jsonobj2.users[i].userRole;
        if (role == 1 || role == 2 || role == 3)
            tablehtml = tablehtml + "<a href ='javascript:removeXML(" + jsonobj2.users[i].userId + ")'><u><font color='red'>Revoke access</font></a> ";

        else tablehtml = tablehtml + "<a href ='javascript:generateXML(" + jsonobj2.users[i].userId + ")'><u><font color='blue'>Assign access</font></a> ";

        tablehtml = tablehtml + "</td>";
        tablehtml = tablehtml + "</tr>";

    }
    tablehtml = tablehtml + "</table>";

    document.getElementById("TableHolder").innerHTML = tablehtml;
});