从URL获取React-Native JSON [英] React-Native JSON fetch from URL

查看:266
本文介绍了从URL获取React-Native JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能找到一些帮助来解决这个问题。

I hope I could find some help to the issue.

我正在使用React Native并尝试从名为Feiertage-API的API中获取一些数据(< a href =https://feiertage-api.de/ =nofollow noreferrer> 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.

尝试使用反馈原生的fetch,给我一个这样的回复:

Trying to use fetch in react native, gives me an response like this:

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('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 (this returns me only the data of the state, once giving the cutoff as a parameter)

还有可能获得JSONP通过添加get参数'callback = mycallbackname'

There is also the possibility to get JSONP by adding the get-parameter 'callback=mycallbackname'

如果尝试显示如下数据:
console.log(response.json())

If trying to show the data like: 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

那么我如何能够从响应中提取数据{....}?

So how am I able 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆