jQuery获取JSON访问结果

jQuery get JSON access result

本文关键字:结果 访问 JSON 获取 jQuery      更新时间:2023-09-26

我正在使用以下命令提取一个json数组:

$.get('URL')
.always(function(data) {
    console.log(data);

访问URL时直接返回以下信息:

{
    "user": {
        "ipaddr": "192.168.37.10.",
        "system_date": "Feb 10 10:38:51 PST 2015"
    },
    "clean_graph_allow": 0,
    "client": {
        "account_information": {
            0: {
                "ID": 7611,
                "accountID": "abcd",
                "label": "Billing",
                "email": "bob@jones.com",
                "firstName": "Bob",
                "lastName": "Jones",
                "company": "Bob inc.",
                "address": "123 Fake Street",
                "city": "Nowhere",
                "province": "CA",
                "postalCode": 91111,
                "country": "US",
                "phone": 5555555555,
                "notes": null,
                "company_type": null
            },
            1: {
                "ID": 7611,
                "accountID": "abcd",
                "label": "Billing",
                "email": "bob@jones.com",
                "firstName": "Bob",
                "lastName": "Jones",
                "company": "Bob inc.",
                "address": "123 Fake Street",
                "city": "Nowhere",
                "province": "CA",
                "postalCode": 91111,
                "country": "US",
                "phone": 5555555555,
                "notes": null,
                "company_type": null
            },
            2: {
                "ID": 7611,
                "accountID": "abcd",
                "label": "Billing",
                "email": "bob@jones.com",
                "firstName": "Bob",
                "lastName": "Jones",
                "company": "Bob inc.",
                "address": "123 Fake Street",
                "city": "Nowhere",
                "province": "CA",
                "postalCode": 91111,
                "country": "US",
                "phone": 5555555555,
                "notes": null,
                "company_type": null
            },
            "contact": {
                "ID": 7611,
                "accountID": "abcd",
                "label": "Billing",
                "email": "bob@jones.com",
                "firstName": "Bob",
                "lastName": "Jones",
                "company": "Bob inc.",
                "address": "123 Fake Street",
                "city": "Nowhere",
                "province": "CA",
                "postalCode": 91111,
                "country": "US",
                "phone": 5555555555,
                "notes": null,
                "company_type": null
            }
        },
        "is_reseller": 300
    },
    "hidden_pages": [],
    "action": "dashboard_api_stats",
    "attacks_today": 4920,
    "blocks": 1816,
    "short_blocks": 101,
    "nullroutes": 314,
    "num_countries": 0,
    "attack_dnsc": 3,
    "attack_pstc": 6,
    "attack_gc": 28,
    "attack_core": 55,
    "attack_tcpc": 8,
    "attack_udpc": 0,
    "regional": {
        "": 100
    }
}

我从$.get或$.getJSON命令中得到的响应如下:

Object {
    readyState: 4,
    getResponseHeader: function,
        getAllResponseHeaders: function,
            setRequestHeader: function,
                overrideMimeType: function…
            }
            abort: function(a) {
                var b = a || u;
                return i && i.abort(b), x(0, b), this
            }
            always: function() {
                return e.done(arguments).fail(arguments), this
            }
            complete: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            done: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            error: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            fail: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            getAllResponseHeaders: function() {
                return 2 === t ? f : null
            }
            getResponseHeader: function(a) {
                var b;
                if (2 === t) {
                    if (!j) {
                        j = {};
                        while (b = Cc.exec(f)) j[b[1].toLowerCase()] = b[2]
                    }
                    b = j[a.toLowerCase()]
                }
                return null == b ? null : b
            }
            overrideMimeType: function(a) {
                return t || (k.mimeType = a), this
            }
            pipe: function() {
                var a = arguments;
                return m.Deferred(function(c) {
                    m.each(b, function(b, f) {
                        var g = m.isFunction(a[b]) && a[b];
                        e[f[1]](function() {
                            var a = g && g.apply(this, arguments);
                            a && m.isFunction(a.promise) ? a.promise().done(c.resolve).fail(c.reject).progress(c.notify) : c[f[0] + "With"](this === d ? c.promise() : this, g ? [a] : arguments)
                        })
                    }), a = null
                }).promise()
            }
            progress: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            promise: function(a) {
                return null != a ? m.extend(a, d) : d
            }
            readyState: 4 responseText: "{"
            user ":{"
            ipaddr ":"
            192.168.37.10.
            ","
            system_date ":"
            Feb 10 10: 38: 51 PST 2015 "},"
            clean_graph_allow ":0,"
            client ":{"
            account_information ":{0:{"
            ID ":7611,"
            accountID ":"
            abcd ","
            label ":"
            Billing ","
            email ":"
            bob@ jones.com ","
            firstName ":"
            Bob ","
            lastName ":"
            Jones ","
            company ":"
            Bob inc.
            ","
            address ":"
            123 Fake Street ","
            city ":"
            Nowhere ","
            province ":"
            CA ","
            postalCode ":91111,"
            country ":"
            US ","
            phone ":5555555555,"
            notes ":null,"
            company_type ":null},1:{"
            ID ":7611,"
            accountID ":"
            abcd ","
            label ":"
            Billing ","
            email ":"
            bob@ jones.com ","
            firstName ":"
            Bob ","
            lastName ":"
            Jones ","
            company ":"
            Bob inc.
            ","
            address ":"
            123 Fake Street ","
            city ":"
            Nowhere ","
            province ":"
            CA ","
            postalCode ":91111,"
            country ":"
            US ","
            phone ":5555555555,"
            notes ":null,"
            company_type ":null},2:{"
            ID ":7611,"
            accountID ":"
            abcd ","
            label ":"
            Billing ","
            email ":"
            bob@ jones.com ","
            firstName ":"
            Bob ","
            lastName ":"
            Jones ","
            company ":"
            Bob inc.
            ","
            address ":"
            123 Fake Street ","
            city ":"
            Nowhere ","
            province ":"
            CA ","
            postalCode ":91111,"
            country ":"
            US ","
            phone ":5555555555,"
            notes ":null,"
            company_type ":null},"
            contact ":{"
            ID ":7611,"
            accountID ":"
            abcd ","
            label ":"
            Billing ","
            email ":"
            bob@ jones.com ","
            firstName ":"
            Bob ","
            lastName ":"
            Jones ","
            company ":"
            Bob inc.
            ","
            address ":"
            123 Fake Street ","
            city ":"
            Nowhere ","
            province ":"
            CA ","
            postalCode ":91111,"
            country ":"
            US ","
            phone ":5555555555,"
            notes ":null,"
            company_type ":null}},"
            is_reseller ":300},"
            hidden_pages ":[],"
            action ":"
            dashboard_api_stats ","
            attacks_today ":4920,"
            blocks ":1816,"
            short_blocks ":101,"
            nullroutes ":314,"
            num_countries ":0,"
            attack_dnsc ":3,"
            attack_pstc ":6,"
            attack_gc ":28,"
            attack_core ":55,"
            attack_tcpc ":8,"
            attack_udpc ":0,"
            regional ":{"
            ":100}}"
            setRequestHeader: function(a, b) {
                var c = a.toLowerCase();
                return t || (a = s[c] = s[c] || a, r[a] = b), this
            }
            state: function() {
                return c
            }
            status: 200 statusCode: function(a) {
                var b;
                if (a)
                    if (2 > t)
                        for (b in a) q[b] = [q[b], a[b]];
                    else v.always(a[v.status]);
                return this
            }
            statusText: "OK"
            success: function() {
                if (h) {
                    var d = h.length;
                    ! function f(b) {
                        m.each(b, function(b, c) {
                            var d = m.type(c);
                            "function" === d ? a.unique && k.has(c) || h.push(c) : c && c.length && "string" !== d && f(c)
                        })
                    }(arguments), b ? e = h.length : c && (g = d, j(c))
                }
                return this
            }
            then: function() {
                var a = arguments;
                return m.Deferred(function(c) {
                    m.each(b, function(b, f) {
                        var g = m.isFunction(a[b]) && a[b];
                        e[f[1]](function() {
                            var a = g && g.apply(this, arguments);
                            a && m.isFunction(a.promise) ? a.promise().done(c.resolve).fail(c.reject).progress(c.notify) : c[f[0] + "With"](this === d ? c.promise() : this, g ? [a] : arguments)
                        })
                    }), a = null
                }).promise()
            }
            __proto__: Object

我对以上格式表示歉意。我不知道如何正确格式化。

无论如何,其中重要的部分是responseText。responseText相当于直接访问URL时返回的数据。我需要能够从中提取特定的元素(例如,attacks_today)。

一些简单的东西,比如:

data.responseText.attacks_today
OR
data['responseText']['attacks_today']

不要工作。我也尝试了各种循环方法,但它们都给了我相同类型的错误:

var arr = data.responseText;                                    
$.each( arr, function( index, entry ) {   

结果:

Uncaught TypeError: Cannot use 'in' operator to search for '1700' in ...

必须有一些简单的方法来访问json元素,除非我弄错了,但我只是没有看到

我使用的是jQuery 1.11.0版本。

谢谢。

您的Ajax正在失败。文档表示,如果请求成功,第一个参数将是xhr对象或数据对象。

jqXHR.always(函数(data|jqXHR,textStatus,jqXHR|errorThrown){});

完整回调选项的替代构造.always()方法替换了已弃用的.complete()方法。

作为对成功请求的响应,函数的参数是与.done()相同:data、textStatus和jqXHR对象。对于失败的请求参数与.fail()的参数相同:jqXHR对象、textStatus和errorThrown。请参阅deferred.always()了解实现细节。

http://api.jquery.com/jquery.ajax/

您看到的对象是XHR对象,因为请求失败。如果你想确保在弄清楚请求失败的原因后总是能得到结果,请使用.done(数据)。