[英]Looping through a JSON Array in JavaScript
Okay so I'm given a large JSON Object from a GET request and it looks like this: 好的,所以我从GET请求中获得了一个大的JSON对象,它看起来像这样:
{
"partners": [
{
"firstName": "Mai",
"lastName": "Dost",
"email": "mdost@hubspotpartners.com",
"country": "United States",
"availableDates": [
"2017-05-29",
"2017-05-31",
"2017-06-01",
"2017-06-07",
"2017-06-08",
"2017-06-09",
"2017-06-11",
"2017-06-12"
]
},
{
"firstName": "Annamae",
"lastName": "Monty",
"email": "amonty@hubspotpartners.com",
"country": "United States",
"availableDates": [
"2017-05-31",
"2017-06-01",
"2017-06-07",
"2017-06-08",
"2017-06-09",
"2017-06-11",
"2017-06-12",
"2017-06-15",
"2017-06-16",
"2017-06-20"
]
},
I'm trying to scan through all the individual partners in this object but I can't seem to get anything returned other than "undefined". 我正在尝试浏览该对象中的所有单个合作伙伴,但是除了“ undefined”外,我似乎无法返回任何东西。 I'm using a for loop to try go into each value and I've asked it to alert me each time so I can see it's contents.
我正在使用for循环尝试输入每个值,并且我要求它每次都提醒我,以便我可以看到它的内容。 Can you guys see what I'm doing wrong?
你们能看到我在做什么吗?
The JavaScript code for this is: JavaScript代码是:
$.getJSON("someURL_IcantDisclose", function (result) {
var data = JSON.stringify(result, null, 2);
document.getElementById('load').innerHTML = data;
for(var i=0; i<data.partners.length;i++){
alert(data.partners[i]);
}
});
You are attempting to loop over the strinigified version of the result - $.getJSON
will return the parsed JSON so you can use result
directly: 您尝试遍历结果的分层版本-
$.getJSON
将返回已解析的JSON,因此您可以直接使用result
:
$.getJSON("someURL_IcantDisclose", function (result) {
for(var i=0; i< result.partners.length; i++){
alert(result.partners[i]);
}
});
Not sure what these two lines are doing var data = JSON.stringify(result, null, 2);document.getElementById('load').innerHTML = data;
不确定这两行在做什么
var data = JSON.stringify(result, null, 2);document.getElementById('load').innerHTML = data;
, but the following snippet will allow you to loop over the partners
,但以下代码段将允许您遍历
partners
$.getJSON("someURL_IcantDisclose", function (result) {
for(var i=0; i<result.partners.length; i++){
alert(result.partners[i]);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.