簡體   English   中英

從 URL 獲取 React-Native JSON

[英]React-Native JSON fetch from URL

我希望我能找到一些幫助解決這個問題。

我正在使用 React Native 並嘗試從名為 Feiertage-API ( https://feiertage-api.de/ ) 的 API 獲取一些數據,該 API 基本上返回(應該返回)德國的官方假期。

嘗試在 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",
}

我的 fetch 調用如下所示:

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

GET-Parameters 是:- jahr=2019(對於年份,我認為這是顯而易見的)- nur_land=hb(通過簡短形式指定什么狀態)

可以通過添加獲取參數“callback=mycallbackname”來獲取 JSONP

如果嘗試顯示數據:console.log(response.json())

結果是:

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

我期待看到的正是此處顯示的數據 -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb

想知道如何從 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