我有以下问题,现在尝试3天我无法解决。我有一个JSON数组:
var geojson = {
type: 'FeatureCollection',
features: [
{
"type": "Feature",
"geometry": {
"coordinates": [
5.140439,
51.686608
],
"type": "Point"
},
"properties": {
"title": "TEST1",
"rentals": true,
"tackleshop": false,
"fuel": false,
"marker-color": "#1087bf",
"marker-size": "large",
"marker-symbol": "harbor"
}
},
{
"type": "Feature",
"geometry": {
"coordinates": [
5.134060,
51.686890
],
"type": "Point"
},
"properties": {
"title": "TEST2",
"rentals": true,
"tackleshop": false,
"fuel": true,
"marker-color": "#1087bf",
"marker-size": "large",
"marker-symbol": "harbor"
}
},
{
"type": "Feature",
"geometry": {
"coordinates": [
5.133729,
51.681425
],
"type": "Point"
},
"properties": {
"title": "TEST3",
"rentals": false,
"tackleshop": true,
"fuel": true,
"marker-color": "#1087bf",
"marker-size": "large",
"marker-symbol": "harbor"
}
}
]
};
我现在测试的是什么:
$.each(geojson, function() {
$.each(this, function(key, value) {
$.each(this, function(value, featuress) {
console.log(featuress.properties.title);
});
});
});
我想要的结果: 我想要一个$ .each,它通过这个json数组,我可以在这里显示变量:" title"对于每个功能。
谁能帮助我?干杯!
答案 0 :(得分:1)
您将JSON对象循环了2次,我删除了外部foreach
循环。在这种情况下,您只能遍历列表 - features
。请参阅代码段:
var geojson = {
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"coordinates":[
5.140439,
51.686608
],
"type":"Point"
},
"properties":{
"title":"TEST1",
"rentals":true,
"tackleshop":false,
"fuel":false,
"marker-color":"#1087bf",
"marker-size":"large",
"marker-symbol":"harbor"
}
}
]
}
$.each(geojson.features, function(i, feature) {
console.log(feature.properties.title);
});
You looped over an object
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
进一步阅读:
答案 1 :(得分:1)
作为您的代码,geojson
是一个json对象,它不是一个json数组。
json数组的示例是[{"name":"item 1"},{"name": "item2} ]
。
要查看列表功能并显示其属性,您可以 试试这个:
$.each(geojson.features, function(index, feature) {
console.log(feature.properties.title);
});