【发布时间】:2017-11-05 13:48:13
【问题描述】:
我是 jquery 的新手 - 我有一个有效的 geojson 文件,我想访问其 features 并将其转换为键值对对象。我的目标是只使用properties.cat 作为键和properties.name 作为值(可以忽略所有其他数据)。这是一个示例:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
$.getJSON("sample.geojson", function(json) {
console.log(json.features[0].properties.cat);
});
正如下面指出的,features 是一个数组。
如何直接从 json 中的每个特征属性创建一个键值对对象,从而获得以下输出:
{A : Aberdeen, B: Berlin, C: Copenhagen, D: Dublin, E: Edinburgh}
【问题讨论】:
-
在解析之前,您是否尝试将 json 变量记录到控制台?
-
var obj 是干什么用的??
-
我已经编辑了这个问题。请重新打开
标签: javascript jquery json ajax geojson