【发布时间】:2016-01-20 20:27:41
【问题描述】:
我有一个外部 JSON 文件。它看起来像这样:
{
"type":"FeatureCollection",
"totalFeatures":1,
"features":
[{
"type":"Feature",
"id":"asdf",
"geometry":null,
"properties":
{
"PARAM1":"19 16 11",
"PARAM2":"31 22 11",
"PARAM3":"12 10 7",
}
}],
"crs":null
}`
我认为“features”是一个 JSON 数组,而“properties”是这个数组的一个对象。 我坚持尝试将元素“PARAM1”“push”放入我的 JS 代码中的另一个数组中。 我的尝试是通过 jQuery AJAX 获取数据。它看起来像这样:
function (){
var arrPARAM1 = new Array ();
$.ajax({
async: false,
url: 'gew.json',
data: "",
accepts:'application/json',
dataType: 'json',
success: function(data) {
arrPARAM1.push(data.features);
}
})
console.log(arrPARAM1);
}
使用 arrPARAM1.push(data.features) 我可以将整个数组“features”“push”到我的 JS 数组中。但我只想从对象“properties”中获得元素“PARAM1”。我怎样才能更深入(features --> properties --> PARAM1)?
感谢您的关注!
解决方案:
arrPARAM1.push(data.features[0].properties.PARAM1);
【问题讨论】:
-
永远不要将 async 设置为 false ......它甚至已被弃用(并且已经有一段时间了)
标签: javascript jquery arrays json ajax