简体   繁体   English

如何显示来自 Web 服务的 json 数据

[英]how to show json data from web service

I have that link http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella that shows on the browser a json with all the values, so I need to pick only the wind, and the temp, but I don't know how to extract it, how to do that?我有那个链接http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella在浏览器上显示一个包含所有值的 json,所以我只需要选择风,然后temp,但我不知道如何提取它,该怎么做?

I thougt in add a callback after "Andorra+la+Vella"&callback=myFunction我想在“Andorra+la+Vella”&callback=myFunction 之后添加一个回调

And develop a code on javascript to get the values that I need, but I do it and something goes wrong, that's a simple test to try this little application and then i add it to my web:并在 javascript 上开发一个代码来获取我需要的值,但是我这样做了并且出现了问题,这是一个简单的测试来尝试这个小应用程序,然后我将它添加到我的网络中:

<!DOCTYPE HTML>
<html lang="es">
<head>
    <meta charset="UTF-8">
<title>Test tiempo andorra</title>
<script type="text/javascript">

    function showTime(obj) {
        var dadIp=document.getElementById('time');
        dadIp.innerHTML+=obj.sunset.wind.speed;
    }
</script>
</head>
<body>
<div id="time">
    <h1>Test of Time in Andorra</h1>
</div>
<script type="text/javascript" src="http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella&callback=showTime"></script>
</body>
</html>

Look at your JavaScript error console.查看您的 JavaScript 错误控制台。

 Uncaught TypeError: Cannot read property 'wind' of undefined

Then look at the JSON source.然后看JSON源码。

obj.sunset doesn't exist. obj.sunset不存在。 obj.sys.sunset does. obj.sys.sunset确实如此。

obj.sunset.wind doesn't exist. obj.sunset.wind不存在。 obj.wind does. obj.wind可以。

See a live example when the correct object paths are used.当使用正确的对象路径时,请参阅实时示例

This way for example:这种方式例如:

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" )
;

In case if you need parse DateTime I advise to use moment.js library.如果您需要解析 DateTime,我建议使用 moment.js 库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM