简体   繁体   English

从 URL 获取 React-Native JSON

[英]React-Native JSON fetch from URL

I hope I could find some help to the issue.我希望我能找到一些帮助解决这个问题。

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 并尝试从名为 Feiertage-API ( https://feiertage-api.de/ ) 的 API 获取一些数据,该 API 基本上返回(应该返回)德国的官方假期。

Trying to use fetch in react native, returns:尝试在 react native 中使用 fetch,返回:

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",
}

My fetch call looks as follows:我的 fetch 调用如下所示:

fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
                .then(response => console.log(response) )
                .catch(error => console.log(error));

GET-Parameters are: - jahr=2019 (for year, I think that's obvious) - nur_land=hb (specifying what state by short form) GET-Parameters 是:- jahr=2019(对于年份,我认为这是显而易见的)- nur_land=hb(通过简短形式指定什么状态)

The possibility is given to get JSONP by adding the get-parameter 'callback=mycallbackname'可以通过添加获取参数“callback=mycallbackname”来获取 JSONP

If trying to show the data with: console.log(response.json())如果尝试显示数据:console.log(response.json())

the result is:结果是:

 Promise {
      "_40": 0,
      "_55": null,
      "_65": 0,
      "_72": null,
   }

What I'm expecting to see is exactly the data which is shown here -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb我期待看到的正是此处显示的数据 -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb

Would like to know how to extract the data out of the Response{....}.想知道如何从 Response{....} 中提取数据。

You need to call .json() method on that response.您需要对该响应调用 .json() 方法。

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));

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

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