【问题标题】:Creating key value pairs from json on import [duplicate]在导入时从 json 创建键值对 [重复]
【发布时间】: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


【解决方案1】:

$.getJson 方法的回调函数已经自动解析了响应。

另外,features 对象是一个数组。使用这个:

console.log(json.features[0].properties.cat);

您可以使用reduce 方法制作键值对,该方法接受callback 提供的方法应用于每个项目。

var json= {
      "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 ] } }
      ]
}
var obj=json.features.reduce(function(obj,item){
  obj[item.properties.cat]=item.properties.name;
  return obj;
},{});
console.log(obj);

【讨论】:

  • 谢谢,我已经编辑了我的问题
  • @the_darkside,看看更新的答案。
  • 感谢@Alexandru,但是当我将它包装在 getJSON 中时它不起作用:$.getJSON("sample.geojson", function(json) { var obj={}; json.features.reduce(function(obj,item){ obj[item.properties.cat]=item.properties.name; return obj; },{}); console.log(obj) });
  • 为什么这在 getJSON 函数中起作用?
  • 因为console.log是在回调reduce完成之前执行的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
相关资源
最近更新 更多