Javascript jQuery获取JSON访问结果
我使用以下命令提取json数组:Javascript jQuery获取JSON访问结果,javascript,jquery,json,Javascript,Jquery,Json,我使用以下命令提取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": {
$.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时返回的数据。我需要能够从中提取特定元素(例如,今天的攻击)
简单地说:
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、文本状态、jqXHR |错误抛出){ }); 完整回调选项的另一个构造是 .always()方法替换不推荐使用的.complete()方法 为了响应成功的请求,函数的参数是 与.done()相同:data、textStatus和jqXHR对象。对于 失败的请求参数与.fail()的参数相同: jqXHR对象、textStatus和error抛出。请参阅deferred.always() 有关实施细节
您看到的对象是XHR对象,因为请求失败。如果您想确保在找出请求失败的原因后总是能得到结果,请使用.done(data)。Matt:在我测试的每种情况下(无论是直接访问数组元素,还是在json数组上循环),get和getJSON有同样的问题。尝试
。然后
而不是。始终
。如果将JSON响应复制到中,它是否有效?尝试解析从ajax接收的数据,例如:data=JSON。解析(数据),这样您将得到一个对象而不是字符串,讨论JSON中的数据类型字段标签必须在中
。所以0:{“ID”:7611…
无效。它应该是“0”:{“ID”:7611…
Paul,是的,几分钟前Mac为我指明了验证JSON的方向后,我才意识到这一点。总是有效的,但是。完成了没有。失败了,所以它返回了一些不好的数据。我将深入研究数据不好的原因。谢谢。确保你在响应头中返回了application/JSON。这是一件事如果您传递给请求的URL是正确的,并且您在浏览器中看到了良好的响应,我可以认为这会破坏它。响应头是正确的。问题是,在代码中的其他地方,完全与我的工作空间分离,一些额外的数据被传递到json返回中。抑制这些额外的内容可以修复d JSON验证,解决了这个问题。谢谢!