转换Geojson嵌套数组数据并在jquery中将其打印为列表

Converting Geojson nested array data and printing it as a list in jquery

本文关键字:打印 列表 jquery 嵌套 Geojson 数组 数据 转换      更新时间:2023-09-26

我正在尝试将GeoJson数据转换为jquery并打印特定值。下面提到的是JSONcode。

 {
 "type": "FeatureCollection",
 "metadata": 
  {
  "generated": 1445482204000,
  "url": "http://earthquake.usgs.gov/fdsnws/event/1/query?  
  format=geojson&starttime=2015-01-01&endtime=2015-01-02",
  "title": "USGS Earthquakes",
  "status": 200,
  "api": "1.0.17",
  "count": 343
  },
 "features": [
  {
  "type": "Feature",
  "properties": {
    "mag": 2.51,
    "place": "21km ESE of Coso Junction, California"
   }
   }
   ]
   }

下面提到的是我的JSON文件,我想打印在jquery中的"特性"数组中的"属性"中出现的键"place"。

下面是我的努力:

    $("#button2").click(function(){
    $.get("http://earthquake.usgs.gov/fdsnws/event/1/count?
    format=geojson&starttime=2015-10-14&endtime=2015-10-21",
    function(data,status){
    $.each(data.features.properties, function (i, ob) {
    $.each(ob, function (ind, obj) {
    console.log("key:" + ind + " value:" + obj);
    });
    });
   });
   });  
HTML:

   <div id="Sesimic_Events">
   <button id="button2">Places affected</button>

下面是我得到的错误

 TypeError: data.features is undefined

任何帮助将非常感激!

所以你的基本错误是在你的$。方法,您正在调用他们的API以获取'http://earthquake.usgs.gov/fdsnws/event/1/count?format=geojson&starttime=2015-10-14&endtime=2015-10-21'的计数,这不会返回您想要的对象,如上面所示。您确实需要ping此地址:http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2015-10-14&endtime=2015-10-21以获取其实际数据,以便您可以遍历并获取信息....

所以应该是

$("#button2").click(function(){
    $.get("http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2015-10-14&endtime=2015-10-21",
    function(data, status) {
    $.each(data.features.properties, function (i, ob) {
        $.each(ob, function (ind, obj) {
           console.log("key:" + ind + " value:" + obj);
        });
    });
  });
});  
但这会带来其他明显的错误,你连接的对象实际上是一个数组,所以是data。特征= [{},{}.{}];所以data。features。properties仍然是undefined。因为我也将使用这些数据,所以我想我会帮助您以您期望的方式解析它:
$.get("http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2015-10-14&endtime=2015-10-21",
function (data, status) {
    $.each(data.features, function(index, object){
        $.each(object.properties, function(i,obj){
            console.log("key: ", i, " value: ", obj);
        });
    });
});

最重要的是不要假设数据是正确的,如果你从get得到错误,只是做一个无害的console.log(data);以确保您的数据是通过预期!