![](/img/trans.png)
[英]How do I assign data from a weather api object to individual html elements using javascript?
[英]How to fetch data from weather rest api using javascript?
Stackoverflow 社区你能帮我如何使用 JavaScript 从天气 rest api 中提取数据吗? 我无法使用此 api 获取天气状况、数据和时间。
{
info: {
_postman_id: "3683bf65-703d-2798-6901-c97ed650c68f",
name: "IOT",
schema: "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
item: [
{
name: "Find",
id: "df046832-3c4d-950c-ae9f-dae36af7ebc7",
request: {
method: "POST",
header: [
{
key: "Content-Type",
value: "application/x-www-form-urlencoded"
}
],
body: {
mode: "urlencoded",
urlencoded: [
{
key: "table",
value: "data",
type: "text"
},
{
key: "search",
value: "{"device":"24: 6f: 28: 7a: 87: c2","date":{"$gt":"time(2021 - 02 - 21T13: 10: 48.593Z)","$lt":"time(2021 - 02 - 21T14: 10: 48.593Z)"}}",
type: "text"
},
{
key: "",
value: "{"device":"24: 6f: 28: 7a: 87: c2","date":{"$lt":"time(Sun Feb 21 2021 16: 59: 53 GMT + 0300(Arabian Standard Time))"}}",
type: "text"
}
]
},
url: "http://185.247.117.33/DB/Find"
},
response: []
}
]
}
我从 api 获取数据的代码
fetch('https://www.postman.com/collections/3175ec392efd66dc9a50')
.then(response => response.json())
.then(data => {
console.log(data.item[0].request.body.urlencoded[1].value)
console.log(data.item[0].request.body.urlencoded[2].value) // Prints result from `response.json()` in getRequest
})
.catch(error => console.error(error))
请任何人帮助我如何在数组时间和天气条件下获取数据。
您需要在使用前解析结果。
fetch('https://www.postman.com/collections/3175ec392efd66dc9a50')
.then(response => response.json())
.then(data => {
let value1 = JSON.parse(data.item[0].request.body.urlencoded[1].value)
let value2 = JSON.parse(data.item[0].request.body.urlencoded[2].value);
console.log ( value1.date, value2.date);
})
.catch(error => console.error(error))
urlencoded
里面的数据是JSON格式的,使用JSON.parse
转换成object;
fetch('https://www.postman.com/collections/3175ec392efd66dc9a50').then(response => response.json()).then(data => { const value = data.item[0].request.body.urlencoded[1].value; const json = JSON.parse(value); console.log(json.device); console.log(json.date['$gt']); }).catch(error => console.error(error)) // Output: // 24:6f:28:7a:87:c2 // time(2021-02-21T13:10:48.593Z)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.