這是我的 JSON 陣列,它與一些個人資料相關,我想從我的反應應用程式訪問這些資料這個 JSON 檔案在我的反應應用程式中
{
"person": [
{
"id": "userId",
"sellerImage": "https://i.pravatar.cc/300",
"lastOnline": "Date of last online",
"sellerName": "Ryann Remo",
"isOnline": true,
"lastSeenDate": "Today",
"product":"Rough Shirt",
"messages": [
{
"id": "messageId",
"userId": "userIdOfUser",
"message": "Message text 01",
"date": "Date of message",
"time": "time of message",
"isRead": false
},
{
"id": "messageId",
"userId": "userIdOfSeller",
"message": "Message text 02",
"date": "Date of message",
"time": "time of message",
"isRead": true
},
{
"id": "messageId",
"userId": "userIdOfSeller",
"message": "Message text",
"date": "Date of message 03",
"time": "time of message",
"isRead": false
}
]
},
{
"id": "userId",
"sellerImage": "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50",
"lastOnline": "Date of last online",
"sellerName": "Karp Bonolo",
"isOnline": true,
"lastSeenDate": "Yesterday",
"product":"Rough Shirt",
"messages": [
{
"id": "messageId",
"userId": "userIdOfUser",
"message": "Message text",
"date": "Date of message",
"time": "time of message",
"isRead": false
},
{
"id": "messageId",
"userId": "userIdOfSeller",
"message": "Message text",
"date": "Date of message",
"time": "time of message",
"isRead": true
},
{
"id": "messageId",
"userId": "userIdOfSeller",
"message": "Message text",
"date": "Date of message",
"time": "time of message",
"isRead": false
}
]
}
]
}
我需要"message": "Message text"從反應應用程式訪問這個 JSON 檔案。我怎樣才能做到這一點 ???
(例如我需要
Message text 01
Message text 02
Message text 03
在我的 React App 中列印我該怎么做???)
只需要一個小例子來訪問這個子資料。我需要在我的 ract 應用程式中列印上面突出顯示的文本。
uj5u.com熱心網友回復:
如果您提供了當前擁有的代碼示例,這可能會有所幫助。但是你可能需要做這樣的事情。
person.messages.map(item => {
return <span>{item.message}</span>
})
uj5u.com熱心網友回復:
您可以在反應應用程式中像這樣訪問它...
YOUR_ARRAY.person.map(firstObject => {
firstObject.messages.map(secondMessageObject => {
return <h2>
{secondMessageObject.message}
</h2>
})
})
如果可行,請在此處接受它作為答案:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/466395.html
標籤:javascript 数组 反应 目的
