简体   繁体   中英

How to only get certain data from API JSON response data

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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