如何在 React Native 中从 REST API 查询 JSON 数据

IT技术 json http reactjs react-native
2021-05-26 07:35:34

我想通过 React Native 中的 NewsAPI 查询一些数据,但不知道该怎么做,而且我看过的教程都没有帮助。谢谢!

1个回答

查看来自 React Native 的网络文档...我在我们的生产应用程序中使用 Fetch。

React Native 为您的网络需求提供了 Fetch API。如果您以前使用过 XMLHttpRequest 或其他网络 API,Fetch 会很熟悉。您可以参考 MDN 的使用 Fetch 指南以获取更多信息。

MDN 获取 API 指南

fetch('http://google.com')
 .then(res => {
    if (res.ok) return res.json())
    else throw new Error(res)
 .then(json => {
   for (var key in json) {
     // now you can parse it by calling json[key]
   }
 .catch(error => console.log(error)
 }