reactjs - 从 JSON react-native 获取特定对象
问题描述
我创建了一个 GET 函数,用于获取要下载的 api,但是在 api 上发出的请求不包含应该下载视频的特定 nameVid 参数,下面是我创建的下载函数,
download =async () => {
try {
const response = await fetch('so url');
const resJson = await response.text();
this.setState({
isLoading: false,
data: resJson.product,
}, console.log(resJson));
}
catch (error) {
console.error(error);
}
}
在向它发出的 api 发出请求但它没有获取确切的内容时,我还应该添加什么才能获取要下载的确切视频?
以下是我返回的 JSON 日志,我必须在其中获取 nameVid 才能下载:
{"product": [{"__v": 0, "_id": "5e301696f75182463c6874ed", "color": "Space Grey", "colors": [Array], "description": "6.5-inch Super Retina XDR OLED display
Water and dust resistant (4 meters for up to 30 minutes, IP68)
Triple-camera system with 12MP Ultra Wide, Wide, and Telephoto cameras; Night mode, Portrait mode, and 4K video up to 60fps
12MP TrueDepth front camera with Portrait Mode, 4K video, and Slo-Mo
Face ID for secure authentication and Apple Pay
A13 Bionic chip with third-generation Neural Engine
Fast charge with 18W adapter included
Wireless charging
Manufacturer Detail: Apple Inc, One Apple Park Way, Cupertino, CA 95014, USA", "downloads": 2, "nameImg": "61jgfLBydjL._SL1024_-1580209807807.jpg", "nameVid": "videoplayback (1)-1580209807809.mp4", "price": 99900, "sellerID": "13755902031", "sellerName": "Appario", "size": "5.8-inch", "sizes": [Array], "title": "Apple iPhone 11 Pro", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-28T11:10:14.244Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/61jgfLBydjL._SL1024_-1580209807807.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback+%281%29-1580209807809.mp4"}, {"__v": 0, "_id": "5e30171df75182463c6874ee", "color": "Haze Blue", "colors": [Array], "description": "Rear Camera - 48MP (Primary) + 8MP (Tele-photo)+16MP (Ultrawide) | Front Camera - 16 MP POP-UP Camera
16.9 centimeters (6.67-inch) multi-touch capacitive touchscreen with 3120 x 1440 pixels resolution
Memory, Storage and SIM: 6GB RAM | 128GB internal memory | Dual SIM dual-standby (4G+4G)
Android Oxygen operating system with 2.84GHz Snapdragon 855 octa core processor
4000mAH lithium-ion battery
1 year manufacturer warranty for device and 6 months manufacturer warranty for in-box accessories including batteries from the date of purchase
Box also includes: Power Adapter, Type-C Cable (Support USB 2.0), Quick Start Guide, Welcome Letter, Safety Information and Warranty Card, Logo Sticker, Case, Screen Protector (pre-applied) and SIM Tray Ejector", "downloads": 41, "nameImg": "61FRLa8IFTL._SL1500_-1580209943294.jpg", "nameVid": "videoplayback-1580209943295.mp4", "price": 53999, "sellerID": "13755902031", "sellerName": "OnePlus", "size": "6.67 inch", "sizes": [Array], "title": "OnePlus 7T Pro", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-28T11:12:29.918Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/61FRLa8IFTL._SL1500_-1580209943294.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback-1580209943295.mp4"}, {"__v": 0, "_id": "5e3273ae32213d4ba037042a", "color": "assas", "colors": [Array], "description": "assa", "downloads": 0, "nameImg": "apex-legends-logo-1580364717181.jpg", "nameVid": "videoplayback (2)-1580364717241.mp4", "price": 222, "sellerID": "sas", "sellerName": "sasa", "size": "sdassa", "sizes": [Array], "title": "wws", "typeImg": "image/jpeg", "typeVid": "video/mp4", "uploadedOn": "2020-01-30T06:11:58.768Z", "urlImg": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/apex-legends-logo-1580364717181.jpg", "urlVid": "https://atiiproductmediafiles.s3.ap-south-1.amazonaws.com/videoplayback%20%282%29-1580364717241.mp4"}], "user": {"__v": 0, "_id": "5e32f1bbe5db08530cae29c8", "address": [], "changes": [], "checkout": [], "like": [], "links": [], "mobile": 8697779335, "orders": [], "registeredOn": "2020-01-30T15:09:47.117Z"}}
请告诉我是否需要其他任何东西,我已经坚持很久了,请帮忙。
解决方案
如果您期望响应为 JSON,则将响应正文解析为 JSON,而不是像您现在一样的文本。
const response = await fetch('url');
const resJson = await response.json(); // .json instead of .text
推荐阅读
- arduino - Arduino键盘破解整个代码(闹钟)
- java - 错误:android.content.res.Resources$NotFoundException:字符串资源 ID #0x1
- node.js - 发布请求无法正常工作,有些数据正在传输,但有些数据没有
- python - 子矩阵上的 Python absdiff 性能
- javascript - Vue 将数据 URI 绑定到 img src 属性。仅当我的其他道具以某种方式发生变化时,浏览器中的图像才会更新
- java - 获取对象Spring Boot Java的属性值
- flutter - Flutter 使用 custompainter 实现像画图一样的绘图游戏的有效方法
- asp.net-core - 如何使用 smtp.gmail.com 和服务帐户的 oauth 访问令牌发送电子邮件?
- php - 逐行选中和取消选中复选框
- xampp - Inno Setup ReadReg运行时目标程序文件夹,将WAR文件添加到tomcat/webapps