`我從 api 呼叫中得到了這個回應,我需要從回應中提取特定值。另外,我想將它用作另一個 api 呼叫中的預請求腳本。
- 如何從 Json 物件中提取 id 的值?2)是否可以提取 id 鍵的特定值,其中鍵等于“[email protected]”?
{
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
`
uj5u.com熱心網友回復:
要決議json,在 JS 中有一種本地方法可以做到這一點。
JSON.parse()
使用示例:
const data = JSON.parse(yourJsonObject);
然后:
console.log(data.content)在您的情況下將回傳:
[
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
}, ...
]
uj5u.com熱心網友回復:
因為你想找到等于“[email protected]”的內容的id,并且內容是一個物件陣列,所以你可以像這樣使用陣列的過濾方法。
const result=response.content.filter(value=>value.from=="[email protected]");
結果變數將包含與您的電子郵件匹配的整個物件,因此如果您只想要 id 則可以使用另一個地圖屬性來獲取它。
const ids=result.map(item=>item.id);
uj5u.com熱心網友回復:
您可以直接訪問 JSON 物件,但如果您想轉換為 JS 物件,請使用 JSON.parse()
//You can access to JSON object, but if you prefer to get JS Object do this: JSON.parse(objJSON)
const objJSON = {
"content": [
{
"id": "e7ab9f7d-c9f4-47e3-8d53-6febcfb914",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dc34221",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T15:43:02.357Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\">id 23543253534<div>sid 34645656452342342343424</div></div>\r\n",
"teamAccess": true,
"bodyMD5Hash": "F2956A8791EB5E6F6F6E259C112BB13B"
},
{
"id": "8d547247-32d2-4553-b1fe-33b4ca00221d2",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: saas",
"inboxId": "af09331a-d681-48c4-9075-2c263f2dc5ba8",
"bcc": [],
"cc": [],
"createdAt": "2022-11-01T22:20:23.301Z",
"read": true,
"bodyExcerpt": "<div dir=\"auto\"><div dir=\"auto\"></div><p style=\"font-size:12.8px\">sid 325sd-df435-3fdgvd435-gdfv43</",
"teamAccess": true,
"bodyMD5Hash": "948B78E301880858EB66ABDE6698450B"
},
{
"id": "446760be-e261-441a-bffe-fa31aa935239",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Complete your registration",
"inboxId": "10ea0b7b-b5eb-4c0f-908d-394354324a71",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T07:41:41.685Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "3A7619478AB69B1F63C99B9716896B1B"
},
{
"id": "79a2c183-5b72-4bc1-98aa-63bf5d52c2e6",
"from": "[email protected]",
"domainId": null,
"attachments": [],
"to": [
"[email protected]"
],
"subject": "Re: id and sid",
"inboxId": "af09331a-d681-48c4-9075-2c2965dbdf5328",
"bcc": [],
"cc": [],
"createdAt": "2022-11-02T19:20:44.655Z",
"read": true,
"bodyExcerpt": "<div dir=\"ltr\"><span style=\"color:unset;font:unset;font-feature-settings:unset;font-kerning:unset;fo",
"teamAccess": true,
"bodyMD5Hash": "1ED8849F70CBCBBA6CF3CFEA0ACA66C4"
}
],
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"last": true,
"totalElements": 4,
"totalPages": 1,
"size": 20,
"number": 0,
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"first": true,
"numberOfElements": 4,
"empty": false
}
//Array of IDs of each content object
const arrayIDs = [];
objJSON.content.forEach( c => {
arrayIDs.push( c.id );
} )
//You can get the object filtering by the attribute you need
const rahulSharma = objJSON.content.find( c => {
return c.from == "[email protected]"
} )
//Then you can access to any attribute. In this case: id
console.log(`Sahul Sharma's ID: ${rahulSharma.id}`)
console.log('All IDs')
console.log(arrayIDs)
console.log('Info of Rahul Sharma')
console.log(rahulSharma)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526609.html
