如何仅从API JSON响应数据中获取某些数据

发布于 2025-02-09 18:27:07 字数 239 浏览 3 评论 0原文

我是JS和JSON的新手,我看了一段时间,似乎找不到答案。我试图使用此API,该API返回两个属性,如下面的代码块中所示,

{
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

它仅返回这两个值,并且每次都会随机化。使用JS,我每次仅抓取“引用”元素。 提前致谢!

I am very new to JS and JSON, I've looked for a while and I can't seem to find the answer. I'm trying to use this API that returns two properties, as seen in the code block below,

{
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

It only returns those 2 values and they are randomized each time. Using JS, how do I only grab the "quote" element each time.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2025-02-16 18:27:07
result['quote']

示例

results = {
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

console.log(results['quote'])

输出

创新区分领导者和追随者。

编辑:

无法实际从您的网站获取

数据

document.onLoad(fetch("https://michael-scott-quotes-api.herokuapp.com/randomQuote")
.then((response)=>{
    data = response.json();
    console.log(data)
})
)

现在,由于CORS,

将 从API获取数据,然后您可以做您想做的事

result['quote']

example

results = {
"quote": "Innovation distinguishes between a leader and a follower.",
"name": "Steve Jobs"
}

console.log(results['quote'])

output

Innovation distinguishes between a leader and a follower.

Edit:

Now I won't be able to actually get the data from your website due to CORS

but you could use the fetch method for it

example

document.onLoad(fetch("https://michael-scott-quotes-api.herokuapp.com/randomQuote")
.then((response)=>{
    data = response.json();
    console.log(data)
})
)

and then you can use the above method to get the quote from it

this will on document load, fetch the data from API and then you can do what you want with it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文