简体   繁体   English

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

[英]How to query JSON data from REST API in React Native

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. 我想查询React Native中NewsAPI的一些数据,却不知道该怎么做,我所见的任何教程都没有帮助。 Thanks! 谢谢!

Check out the Docs on Networking from React Native... I'm using Fetch in our production apps. 查看来自React Native的网络文档 ...我正在生产应用程序中使用Fetch。

React Native provides the Fetch API for your networking needs. React Native提供了Fetch API来满足您的网络需求。 Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. 如果您以前使用过XMLHttpRequest或其他网络API,则提取似乎很熟悉。 You may refer to MDN's guide on Using Fetch for additional information. 您可以参考MDN关于使用提取的指南以获取更多信息。

MDN Fetch API Guide MDN Fetch 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)
 }

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

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