在本机反应中从 json 对象访问特定数据
Posted
技术标签:
【中文标题】在本机反应中从 json 对象访问特定数据【英文标题】:Accessing specific data from a json object in react native 【发布时间】:2021-11-07 04:02:07 【问题描述】:我正在尝试从 json 对象中获取单个值。例如,只获取“作者”而不是整个对象
这是我当前的代码:
const getArticlesFromApi = async () =>
let response = await fetch(
'http://api.mediastack.com/v1/news'
)
let test = await response.json();
console.log(test)
可以对上面的代码做些什么来达到我想要的结果 这是当前的输出:
Object
"author": "OdAdmin",
"category": "general",
"country": "us",
"description": "New Delhi: Overcoming all hurdles and finding new solutions, Indian Railways is continuing its journey of bringing relief by delivering Liquid Medical Oxygen(LMO) to various states across the country. Oxygen Expresses crossed mark of 26000 MT of LMO delivery in service to the Nation. So far, Indian Railways has delivered more than 26891 MT of […]",
"image": null,
"language": "en",
"published_at": "2021-06-07T15:37:44+00:00",
"source": "orissadiary",
"title": "Oxygen Expresses deliver more than 26891 MT of LMO to the Nation",
"url": "https://orissadiary.com/oxygen-expresses-deliver-more-than-26891-mt-of-lmo-to-the-nation/",
,
Object
"author": "Graham Gremore",
"category": "general",
"country": "us",
"description": "We've officially lost count of how many times Rand Paul has claimed to be the victim of some sort of foiled assassination plot.",
"image": null,
"language": "en",
"published_at": "2021-06-07T15:39:05+00:00",
"source": "queerty",
"title": "Rand Paul claims his life is in danger (again), says “I don’t know what the world’s coming to!”",
"url": "https://www.queerty.com/rand-paul-claims-life-danger-says-dont-know-worlds-coming-20210607",
,
【问题讨论】:
【参考方案1】:如果 api 没有像您想要的“作者”那样为过滤或投影输出提供任何选项,您需要通过任何类型的迭代方法对 test
变量进行后处理。
例如
let authors = test.map( x => x.author )
【讨论】:
以上是关于在本机反应中从 json 对象访问特定数据的主要内容,如果未能解决你的问题,请参考以下文章