显示来自USGS地震JSON数据的元数据

时间:2017-10-26 08:07:57

标签: javascript json metadata

- 我正在创建一个地震地图网站www.livehazards.com

- 我在拉。我的数据来自UGSG网站(GeoJson格式)。 " https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson"

功能的featurecollection /元数据是 { type:" FeatureCollection", 元数据:{ 生成:长整数, url:String, 标题:字符串, api:String, count:整数 状态:整数 },

  • 我想在侧栏中显示/打印计数器整数但不知道使用什么代码来执行此操作。

任何人都有帮助,请?

2 个答案:

答案 0 :(得分:1)

这里,我准备了一个示例,其中我将您的示例5要素对象粘贴到JSON字符串(缩小)并获得结果。 你也可以申请。

<!DOCTYPE html>
<html>
<body>
<script>
// JSON.parse(<JSON String>); // Json string is minified
var obj = JSON.parse('{"type":"FeatureCollection","metadata":{"generated":1509005451000,"url":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson","title":"USGS Magnitude 1.0+ Earthquakes, Past Month","status":200,"api":"1.5.8","count":5681},"features":[{"type":"Feature","properties":{"mag":0.99,"place":"2km SE of The Geysers, California","time":1509005085910,"updated":1509005181998,"tz":-480,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/nc72914101","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72914101.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":15,"net":"nc","code":"72914101","ids":",nc72914101,","sources":",nc,","types":",geoserve,nearby-cities,origin,phase-data,","nst":7,"dmin":0.01315,"rms":0.01,"gap":159,"magType":"md","type":"earthquake","title":"M 1.0 - 2km SE of The Geysers, California"},"geometry":{"type":"Point","coordinates":[-122.7351685,38.7636681,1.04]},"id":"nc72914101"},{"type":"Feature","properties":{"mag":1.01,"place":"14km ESE of Mammoth Lakes, California","time":1508996258570,"updated":1509000963664,"tz":-480,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/nc72914056","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72914056.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":16,"net":"nc","code":"72914056","ids":",nc72914056,","sources":",nc,","types":",geoserve,nearby-cities,origin,phase-data,scitech-link,","nst":8,"dmin":0.01545,"rms":0.03,"gap":96,"magType":"md","type":"earthquake","title":"M 1.0 - 14km ESE of Mammoth Lakes, California"},"geometry":{"type":"Point","coordinates":[-118.8184967,37.6030006,4.35]},"id":"nc72914056"},{"type":"Feature","properties":{"mag":1.6,"place":"62km WNW of Beatty, Nevada","time":1508993349830,"updated":1508993549009,"tz":-480,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/nn00610589","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nn00610589.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":39,"net":"nn","code":"00610589","ids":",nn00610589,","sources":",nn,","types":",geoserve,origin,phase-data,","nst":31,"dmin":0.195,"rms":null,"gap":136.69,"magType":"ml","type":"earthquake","title":"M 1.6 - 62km WNW of Beatty, Nevada"},"geometry":{"type":"Point","coordinates":[-117.4003,37.13,5.5]},"id":"nn00610589"},{"type":"Feature","properties":{"mag":2.03,"place":"1km ESE of Pahala, Hawaii","time":1508989731730,"updated":1508989927780,"tz":-600,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/hv61956446","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/hv61956446.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":63,"net":"hv","code":"61956446","ids":",hv61956446,","sources":",hv,","types":",geoserve,origin,phase-data,","nst":42,"dmin":0.04174,"rms":0.19,"gap":105,"magType":"md","type":"earthquake","title":"M 2.0 - 1km ESE of Pahala, Hawaii"},"geometry":{"type":"Point","coordinates":[-155.466507,19.1971664,33.47]},"id":"hv61956446"},{"type":"Feature","properties":{"mag":2.07,"place":"24km ENE of Soledad, California","time":1508987976180,"updated":1508999403619,"tz":-480,"url":"https://earthquake.usgs.gov/earthquakes/eventpage/nc72914021","detail":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72914021.geojson","felt":null,"cdi":null,"mmi":null,"alert":null,"status":"automatic","tsunami":0,"sig":66,"net":"nc","code":"72914021","ids":",nc72914021,","sources":",nc,","types":",geoserve,nearby-cities,origin,phase-data,scitech-link,","nst":18,"dmin":0.06662,"rms":0.05,"gap":130,"magType":"md","type":"earthquake","title":"M 2.1 - 24km ENE of Soledad, California"},"geometry":{"type":"Point","coordinates":[-121.0631638,36.4811668,3.99]},"id":"nc72914021"}],"bbox":[-179.9476,-61.9084,-2.93,179.9778,71.4011,614.26]}');

alert(obj.features.length);
</script>

</body>
</html>

希望,这是有效的。谢谢:))

答案 1 :(得分:1)

&#13;
&#13;
<!DOCTYPE html>
<html>
<body>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<script>
$.getJSON("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson", function(json) 
          {
console.log(json);  
var newString = JSON.stringify(json, null, 0);
var obj = JSON.parse(newString);
document.getElementById("amount_of_earthquake").innerHTML = obj.features.length;
          });
</script>
<div id="amount_of_earthquake"></div>
</body>
</html>
&#13;
&#13;
&#13;

希望,它的作品。谢谢:))