javascript - JQuery 访问一些 JSON 数据

标签 javascript jquery json

我已经为此工作了大约一个小时,但似乎无法弄清楚。

JSON 响应:

{"lastDate":"2013-11-22 00:00:35",
"lastId":"42460",
"response":[
{
"class":"rowgreen",
"id":"42460","date":"22 November 2013, 00:00:35\\u0026nbsp;",
"player":"\\u003Ca href=\\u0027logpersonal.php?playerName=skiwi2\\u0027\\u003Eskiwi2\\u003C\/a\\u003E\\u0026nbsp;",
"target":"\\u003Ca href=\\u0027logpersonal.php?playerName=UnholiestElite\\u0027\\u003EUnholiestElite\\u003C\/a\\u003E\\u0026nbsp;",
"weapon":"M1014 (\\u003Cb\\u003EHeadshot\\u003C\/b\\u003E)\\u0026nbsp;",
"server":"Test\\u0026nbsp;"
}
]}

这似乎是正确的,现在是 jquery:

function longPolling() {
    if (!longPollingAllowed) {
        return;
    }
    console.log("Long polling started.");
    $.ajax({
        url: "logpersonal_ajax.php",
        data: {
            serverId: serverId,
            playerName: playerName,
            lastDate: lastDate,
            lastId: lastId
        },
        cache: false,
        dataType: "json",
        beforeSend: function() {
            longPollingBusy = true;
        },
        success: function(json) {
            console.log("success");
            $(json).each(function() {
                console.log("this.lastDate = " + this.lastDate);
                console.log("this.lastId = " + this.lastId)
                console.log("this.response = " + this.response);
                console.log(this.response);
                this.lastDate = this.lastDate;
                this.lastId = this.lastId;
                if (this.response != "") {
                    this.response.each(new function() {
                        console.log(this);
                        var clazz = this.class;
                        console.log("clazz = " + clazz);
                        var id = this.id;
                        var date = this.date;
                        var player = this.player;
                        var target = this.target;
                        var weapon = this.weapon;
                        var server = this.server;
                        var string = "\t\t\t<tr class='" + clazz + "' id='" + id + "'><td>" + date + "</td><td>" + player + "</td><td>" + target + "</td><td>" + weapon + "</td><td>" + server + "</td></tr>\n";
                        console.log("string = " + string);
                        $(string).insertBefore($("#list tr.header").next());               
                    });
                }
            });
            if (lastDate != "" && lastId != "") {
                //longPolling(serverId, playerName, lastDate);
            }
            longPollingBusy = false;
        },
        error: function(json, message) {
            console.log("fail: " + message);
            longPollingBusy = false;
        }
    });    
}

console.log("this.lastDate = " + this.lastDate);有效,this.lastId 也有效。 。 this.response也可以工作并很好地显示以索引 0 开头的数组当展开它时,您可以在开发人员 View 中看到所有元素。

现在是我似乎无法理解的部分:在 foreach超过this.response它不会为 this 打印任何有用的内容(原型(prototype)骨架除外) .

如何访问这些值?

最佳答案

this.response.each(new function() {

这一行是错误的。应该是:

this.response.forEach(function() {

附注我建议使用 $.each(json, function(){ 而不是 $(json).each(function() {

关于javascript - JQuery 访问一些 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20153569/

相关文章:

javascript - Node.js 中的 Jquery 错误 '$ is not a function'

jQuery UI : On drop, 将 div 附加到其可放置目标

javascript - 加载 json 后,在 AngularJS 应用程序中向 HTML 文档添加一个类?

json - 从 json 主体访问键的值

javascript - jQuery/Javascript - 创建文档系统

java - Dropwizard : Exception Handling, 向客户端提供自定义错误 JSON 错误消息

javascript - Codemirror 自动完成插件事件绑定(bind)

javascript - 连接几个 javascript 文件时出现问题

javascript - 如何向 jquery 视差添加参数

javascript - 表 td 上的 onblur 事件