如何在React Native中从REST API查询JSON数据 [英] How to query JSON data from REST API in React Native

查看:134
本文介绍了如何在React Native中从REST API查询JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在React Native中查询来自NewsAPI的一些数据,并且不知道如何这样做,而且我看过的任何教程都没有帮助。谢谢!

I want to query through some data from NewsAPI in React Native and have no idea how to do so and none of the tutorials I've seen have helped. Thanks!

推荐答案

查看文档 ...我在我们的生产应用程序中使用Fetch。

Check out the Docs on Networking from React Native... I'm using Fetch in our production apps.


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

React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.

MDN Fetch API Guide

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

这篇关于如何在React Native中从REST API查询JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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