如何访问 json 对象

我正在尝试创建网页,它使用来自 Breaking bad website 的 APi,并且从这个网站我收到了 JSON 格式的数据,我尝试了很多,但不明白,我如何才能只访问“作者”的对象" 是 "Walter White" 这是接收到的数据。

[{"quote_id":1,"quote":"我没有危险,Skyler。我就是危险!","author":"Walter White","series":"Breaking Bad"},{"quote_id ":2,"quote":"远离我的领域。","author":"Walter White","series":"Breaking Bad"},{"quote_id":3,"quote":"IFT" ,"author":"Skyler White","series":"Breaking Bad"},{"quote_id":4,"quote":"我看着简死去。我在那儿。我看着她死去。我看着她过量并窒息而死。我本可以救她。但我没有。","author":"Walter White","series":"Breaking Bad"},{"quote_id":5,"quote": “说我的名字。”,”author":"Walter White","series":"Breaking Bad"}]


慕娘9325324
浏览 146回答 2
2回答

Qyouu

您可以在此处使用数组.filter()方法,例如:var data = [{quote_id:1,quote:"I am not in danger, Skyler. I am the danger!",author:"Walter White",series:"Breaking Bad"},{quote_id:2,quote:"Stay out of my territory.",author:"Walter White",series:"Breaking Bad"},{quote_id:3,quote:"IFT",author:"Skyler White",series:"Breaking Bad"},{quote_id:4,quote:"I watched Jane die. I was there. And I watched her die. I watched her overdose and choke to death. I could have saved her. But I didn’t.",author:"Walter White",series:"Breaking Bad"},{quote_id:5,quote:"Say my name.",author:"Walter White",series:"Breaking Bad"}];var res = data.filter(d => d.author === 'Walter White')console.log( res ).as-console-wrapper { max-height: 100% !important; top: 0; }

郎朗坤

您可以使用filter. 注意toLowerCase()用于不区分大小写的结果。const filterKey = 'walter white';let data = [{  "quote_id": 1,  "quote": "I am not in danger, Skyler. I am the danger!",  "author": "Walter White",  "series": "Breaking Bad"}, {  "quote_id": 2,  "quote": "Stay out of my territory.",  "author": "Walter White",  "series": "Breaking Bad"}, {  "quote_id": 3,  "quote": "IFT",  "author": "Skyler White",  "series": "Breaking Bad"}, {  "quote_id": 4,  "quote": "I watched Jane die. I was there. And I watched her die. I watched her overdose and choke to death. I could have saved her. But I didn’t.",  "author": "Walter White",  "series": "Breaking Bad"}, {  "quote_id": 5,  "quote": "Say my name.",  "author": "Walter White",  "series": "Breaking Bad"}].filter(item => item.author.trim().toLowerCase() === filterKey);console.log(data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript