使用Jquery获取JSON数据以构建一个显示未定义数据的表

使用Jquery获取JSON数据以构建一个显示未定义数据的表,jquery,json,Jquery,Json,我有一个JSON,看起来像这样 { secInfo: [ { loginResult: 1, userId: "admin", userName: "admin", userpassword: "ramshyam" }, { address: "", loginResult: 1, userEmail: "", userId: "anuraag", userName: "Anuraag", userpassword: "kk0903" } ] }

我有一个JSON,看起来像这样

{
secInfo: [
 {
  loginResult: 1,
  userId: "admin",
  userName: "admin",
  userpassword: "ramshyam"
 },
 {
  address: "",
  loginResult: 1,
  userEmail: "",
  userId: "anuraag",
  userName: "Anuraag",
  userpassword: "kk0903"
 }
]
}
当我使用下面的jquery代码显示表中的数据时,它会在表中填充“undefined”


$(文档).ready(函数(){
//将jQuery live事件附加到按钮
$('#submitlogin').live('click',function()){
$.getJSON('/acc/services/json/security/loginresult.json',函数(数据){
//警报(数据);//取消对此的注释以进行调试
//警报(data.item1+“”+data.item2+“”+data.item3);//进一步调试
//$('#showdata').html(“Result=“+data.loginResult+”userid=“+data.userid+”username=“+data.username+”

”); $.each(data.secInfo,function(){ $('#usertable')。追加( 功能(本){ 返回“”+ “”+此用户ID+“”+ “”+此。用户名+“”+ “”+此用户密码+“”+ "1"+ ""; } ); }) }); }); });
我是jquery的新手,从这篇文章中得到了一些想法


请帮我解决问题

问题可能是函数回调中的“this”,这是一个保留关键字。还可以尝试将JSON条目作为数组而不是类属性进行访问。

我想“this”的范围已经改变了。试试这个:

$(document).ready(function(){
    //attach a jQuery live event to the button
    $('#submitlogin').live('click', function(){
        $.getJSON('/acc/services/json/security/loginresult.json', function(data) {
            //alert(data); //uncomment this for debug
            //alert (data.item1+" "+data.item2+" "+data.item3); //further debug
            // $('#showdata').html("<p>Result="+data.loginResult+" userid="+data.userId+" username="+data.userName+"</p>");
            $.each(data.secInfo, function(dataItem){
                $('#usertable').append(
                    function() {
                        return "<tr>"+
                                    "<td>"+dataItem.userId+"</td>"+
                                    "<td>"+dataItem.userName+"</td>"+
                                    "<td>"+dataItem.userpassword+"</td>"+
                                    "<td>1</td>"+
                                "<tr>"; 
                    }
                );
            })
        });
    });
});
$(文档).ready(函数(){
//将jQuery live事件附加到按钮
$('#submitlogin').live('click',function()){
$.getJSON('/acc/services/json/security/loginresult.json',函数(数据){
//警报(数据);//取消对此的注释以进行调试
//警报(data.item1+“”+data.item2+“”+data.item3);//进一步调试
//$('#showdata').html(“Result=“+data.loginResult+”userid=“+data.userid+”username=“+data.username+”

”); $.each(data.secInfo,函数(dataItem){ $('#usertable')。追加( 函数(){ 返回“”+ “”+dataItem.userId+“”+ “”+数据项。用户名+“”+ “”+dataItem.userpassword+“”+ "1"+ ""; } ); }) }); }); });
这绝对有效

<script>
$(document).ready(function(){
    //attach a jQuery live event to the button
    $('#submitlogin').live('click', function(){
        $.getJSON('/acc/services/json/security/loginresult.json', function(data) {

     var genericlist =  eval('(' + data+ ')');

var table = document.getElementById("usertable");
var tabledata = "";

for(i=0;i<genericlist.secInfo.length;i++){

tabledata += "<tr>";
tabledata += "<td>" + genericlist.secInfo[i].useID += "</td>";
tabledata += "<td>" + genericlist.secInfo[i].useName += "</td>";
tabledata += "<td>" + genericlist.secInfo[i].usePassword += "</td>";
tabledata += "</tr>";

}

table.innerHTML = tabledata;

$(文档).ready(函数(){
//将jQuery live事件附加到按钮
$('#submitlogin').live('click',function()){
$.getJSON('/acc/services/json/security/loginresult.json',函数(数据){
var genericlist=eval(“(“+data+”)”);
var table=document.getElementById(“usertable”);
var tabledata=“”;

对于(i=0;ii),如果仍然不起作用,请在回调中尝试console.log(this),或者将其更改为另一个变量名,然后查看web inspector控制台输出的内容。好的,由于javascript对象都是设计的关联数组,所以像这样访问它可能更容易['userId']。此外,“this”指的是不同的作用域:)
$(document).ready(function(){$('#submitlogin').live('click',function(){$.getJSON('/acc/services/json/security/loginresult.json',function(data){$.each(data.secInfo,function(){console.log(this);alert(this.userId);});});
这在控制台上写得很好,并使用userid发出警报。不知道为什么它没有在函数中获取对象
<script>
$(document).ready(function(){
    //attach a jQuery live event to the button
    $('#submitlogin').live('click', function(){
        $.getJSON('/acc/services/json/security/loginresult.json', function(data) {

     var genericlist =  eval('(' + data+ ')');

var table = document.getElementById("usertable");
var tabledata = "";

for(i=0;i<genericlist.secInfo.length;i++){

tabledata += "<tr>";
tabledata += "<td>" + genericlist.secInfo[i].useID += "</td>";
tabledata += "<td>" + genericlist.secInfo[i].useName += "</td>";
tabledata += "<td>" + genericlist.secInfo[i].usePassword += "</td>";
tabledata += "</tr>";

}

table.innerHTML = tabledata;