如何在简单的html页面中显示API调用中的特定值?

时间:2016-10-01 01:55:25

标签: javascript html json

我有非常基本的HTML知识,几乎没有javascript或jquery等知识。关于SO的大多数答案都是指那些,所以我发布这个问题,希望有人可以指导我。我使用过像R和python这样的语言的API,但我们正在寻找一种轻量级的解决方案。我有一个静态API url,它返回一个json对象。

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY

我想要一个非常简单的html页面,它在打开时获取数据并显示json对象中的一个字段。在这种情况下持续时间:文本。每次我想要最新的值时我都可以刷新页面,但是每x秒一次自动刷新页面会非常棒。

1 个答案:

答案 0 :(得分:0)



var a = setInterval(()=>fetch("https://query.yahooapis.com/v1/public/yql?q=select%20rows.elements.duration.text%20from%20json%20where%20url%3D'https%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fdistancematrix%2Fjson%3Funits%3Dimperial%26origins%3DWashington%2CDC%26destinations%3DNew%2BYork%2BCity%2CNY'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
.then(r=>r.json()).then(b=>console.log(b.query.results.json.rows.elements.duration.text)),2000);

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
&#13;
&#13;
&#13;

这对你来说如何?