我有一个包含我所有数据的JSON文件,我想在我的脚本中将其读取到一个数组中。下面你可以看到一个尝试,但似乎“道具”实际上并没有获取数据。因为我是javascript的新手我猜测有一些简单的遗漏,希望有人能告诉我在加载数组时我做错了什么。
如果我在循环内调用alert(记录),它会在每个循环中输出“0”,“1”,“2”递增,如果我尝试提醒(record.Address1)它返回“undefined”,这是怎么回事?工作?
var props = [];
$.getJSON('http://someurl/auction.json', function(json) {
for (var record in json)
{
props.push(
record.Address1,
record.City,
record.State,
record.Zip,
record.Bed,
record.Bath,
record.Price,
record.LotSize,
record.SQFT,
record.YearBuilt,
"10%",
record.latitude,
record.longitude,
record.Description
);
}
});
以下是JSON文件中的前几个记录
[
{
"TrusteeNumber":"WA-14-611878-TC",
"Date":"8/8/2014 8:00",
"Address1":"7616 East Nora Avenue",
"Address2":"",
"County":"Spokane",
"State":"WA",
"Zip":99212,
"Description":"Description for 7616 East Nora Avenue, Spokane Valley, Spokane, WA 99212\n\n\n\n\n \n Important Documents \n\n\nTrustee Sale Terms and Conditions \n\nSample Trustee Deed Upon Sale \n\nSample Certificate of Sale/Receipt \n\nIRS Form 8300",
"City":"Spokane Valley",
"AssetType":"Residential",
"PropertyType":"SFR",
"Beds":3,
"Baths":1,
"YearBuilt":1950,
"SQFT":"1,160",
"LotSize":0.25,
"APN":"45073 0265",
"EventItemNumber":"E2010-320",
"PropertyID":1706957,
"Image":"http://cdn.mlhdocs.com/rcp_files/auctions/E-2010/photos/thumbnails/155290117-1_bigThumb.jpg",
"latitude":47.672913,
"longitude":-117.301561
},
{
"TrusteeNumber":"WA-14-611501-TC",
"Date":"8/8/2014 8:00",
"Address1":"235 W Columbia Ave",
"Address2":"",
"County":"Spokane",
"State":"WA",
"Zip":99205,
"Description":"Description for 235 W Columbia Ave, Spokane, Spokane, WA 99205\n\n\n\n\n \n Important Documents \n\n\nTrustee Sale Terms and Conditions \n\nSample Trustee Deed Upon Sale \n\nSample Certificate of Sale/Receipt \n\nIRS Form 8300",
"City":"Spokane",
"AssetType":"Residential",
"PropertyType":"SFR",
"Beds":3,
"Baths":3,
"YearBuilt":1947,
"SQFT":"3,151",
"LotSize":0.15,
"APN":"36311 3601",
"EventItemNumber":"E2010-307",
"PropertyID":1707641,
"Image":"http://cdn.mlhdocs.com/rcp_files/auctions/E-2010/photos/thumbnails/208185496-1_bigThumb.jpg",
"latitude":47.7107249,
"longitude":-117.415536
}]
答案 0 :(得分:1)
用于迭代json
变量的循环在其他编程语言中的行为与foreach不同。相反,它表现得像普通的for循环,它将遍历via index。因此,要使用json
集合,您需要修改代码,如下所示:
for (var record in json)
{
props.push(
json[record].Address1,
json[record].City,
json[record].State,
....
);
}
或者你也可以在下面尝试,这与其他编程语言中的foreach一样:
$(json).each(function(index, record) {
props.push(
record.Address1,
record.City,
record.State,
....
);
});
希望这会有所帮助。
答案 1 :(得分:0)
使用for (var record in json)
,record
实际上不是记录,因为它的警报很明显,它所做的是枚举对象中的属性名称,因为json是一个数组记录,该数组的索引
如果您想要数组的记录,则必须执行json[record].Address1
。
我还建议不要在数组中使用for循环,
for (i = 0; i < json.length; i++)
更清楚地表明你正在循环一个数组
答案 2 :(得分:0)
您的for循环未正确使用,在for( var record in json )
中,record是索引,在本例中为0, 1
。让它工作试试这个:
for (var key in json)
{
var record = json[key];
props.push(
record.Address1,
record.City,
record.State,
record.Zip,
record.Bed,
record.Bath,
record.Price,
record.LotSize,
record.SQFT,
record.YearBuilt,
"10%",
record.latitude,
record.longitude,
record.Description
);
答案 3 :(得分:0)
我已经在代码中的几个不同位置警告了props变量,并注释了它在哪里为null以及它在哪里有内容。我还在我解决问题的那一行添加了评论。基本上调用函数,而props仍然有一个值,并从那里将该数据分配给全局变量。如果没有函数,则无法直接分配给全局。
<script type="text/javascript">
var props = [], returnData;
function initialize() {
$(document).ready(function() {
$.ajax({
url: "http://5i2.us/jacksonmashup/allResults.json",
async: false,
dataType: 'json',
success: function (data) {
returnData = data;
}
});
var len = returnData.length;
for (var i = 0; i < len; i++){
var record = returnData[i];
props.push([
'<img src="' + record.Type + '"/>',
record.Address1,
record.City,
record.State,
record.Zip,
record.Beds,
record.Baths,
record.Price,
record.SQFT,
record.LotSize,
'10%',
record.latitude,
record.longitude,
record.Description,
record.Image,
record.Type]
)
};
});
}
</script>