React-Native JSON fetch from URL(从 URL 获取 React-Native JSON)
问题描述
我希望我能找到一些帮助解决这个问题.
I hope I could find some help to the issue.
我正在使用 React Native 并尝试从名为 Feiertage-API (https://feiertage-api.de/),基本上返回(应该返回)德国的官方假期.
I'm using React Native and try to get some data from an API called Feiertage-API (https://feiertage-api.de/), that basically returns (should return) the official holidays in Germany.
尝试在 react native 中使用 fetch,返回:
Trying to use fetch in react native, returns:
Response {
"_bodyBlob": Blob {
"_data": Object {
"blobId": "49C4AD4B-4648-44DB-AED7-7654EB78EF7A",
"name": "api",
"offset": 0,
"size": 487,
"type": "application/json",
},
},
"_bodyInit": Blob {
"_data": Object {
"blobId": "49C4AD4B-4648-44DB-AED7-7654EB78EF7A",
"name": "api",
"offset": 0,
"size": 487,
"type": "application/json",
},
},
"headers": Headers {
"map": Object {
"access-control-allow-origin": Array [
"*",
],
"content-type": Array [
"application/json",
],
"date": Array [
"Tue, 31 Jul 2018 07:17:51 GMT",
],
"server": Array [
"nginx",
],
"x-powered-by": Array [
"PHP/7.0.31, PleskLin, PleskLin",
],
},
},
"ok": true,
"status": 200,
"statusText": undefined,
"type": "default",
"url": "https://feiertage-api.de/api/?jahr=2019&nur_land=hb",
}
我的 fetch 调用如下所示:
My fetch call looks as follows:
fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
.then(response => console.log(response) )
.catch(error => console.log(error));
GET-参数为:- jahr=2019(对于年份,我认为这很明显)- nur_land=hb(以简写形式指定什么状态)
GET-Parameters are: - jahr=2019 (for year, I think that's obvious) - nur_land=hb (specifying what state by short form)
可以通过添加 get-parameter 'callback=mycallbackname' 来获取 JSONP
The possibility is given to get JSONP by adding the get-parameter 'callback=mycallbackname'
如果尝试显示数据:console.log(response.json())
If trying to show the data with: console.log(response.json())
结果是:
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
我期望看到的正是此处显示的数据 -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb
What I'm expecting to see is exactly the data which is shown here -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb
想知道如何从 Response{....} 中提取数据.
Would like to know how to extract the data out of the Response{....}.
推荐答案
您需要对该响应调用 .json() 方法.
You need to call .json() method on that response.
fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
.then(response => response.json() )
.then(data => console.log(data) )
.catch(error => console.log(error));
这篇关于从 URL 获取 React-Native JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 URL 获取 React-Native JSON
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01