我如何从以下 json 响应中获取标题或正文?{ "message": "All Books", "data": { "current_page": 1, "data": [ { "id": 2, "title": "The Quran", "slug": "the-quran", "body": "The Holy Quran", "book": "/storage/books/1602685137.pdf", "cover": "/storage/covers/1602685137.jpg", "os": "both", "price_ios": 0, "price_android": 0, "created_at": "2020-10-14T14:18:57.000000Z", "updated_at": "2020-10-14T14:18:57.000000Z" } ], "first_page_url": "https://book.test/api/v1/books?page=1", "from": 1, "last_page": 1, "last_page_url": "https://book.test/api/v1/books?page=1", "links": [ { "url": null, "label": "Previous", "active": false }, { "url": "https://book.test/api/v1/books?page=1", "label": 1, "active": true }, { "url": null, "label": "Next", "active": false } ], "next_page_url": null, "path": "https://book.test/api/v1/books", "per_page": 15, "prev_page_url": null, "to": 1, "total": 1 }}
2 回答
至尊宝的传说
TA贡献1789条经验 获得超10个赞
假设 json 存储在变量res中。
由于第二个数据对象存储为数组[]。
title = res['data']['data'][0]['title']; body = res['data']['data'][0]['body'];
如果发现数组有多个对象的情况,则必须将 0 更改为对象在数组中的索引。
呼唤远方
TA贡献1856条经验 获得超11个赞
假设您的 JSON 已分配给一个名为的变量,response
您可以通过以下方式访问正文:let body = response.data.data[0].body
和标题let title = response.data.data[0].title
如果您想遍历数据数组以获取所有条目(例如标题)的值,请尝试以下操作:let titles = response.data.data.forEach(entry => console.log(entry.title));
添加回答
举报
0/150
提交
取消