When I did
console.log(JSON.parse(JSON.stringify(e)).data);
I got
{"deviceId":"1234","instanceId":"drogon","operationalEvent":"Shutdown","subEventReason":"Finished","operationalState":"in shutdown","createdAt":"2019-06-07 15:22:17","initiator":"system"}
When I did
console.log(JSON.parse(JSON.stringify(e)).data.deviceId);
I got
app.js:10254 undefined
What did I do wrong ?
console.log(typeof JSON.parse(JSON.stringify(e))) //object
console.log(typeof JSON.parse(JSON.stringify(e)).data) //string
JSON.parse(JSON.stringify(e))
is nonsense, it's the same as just the original object e
to begin with. Since you say that e.data
is a string, that's what you need to parse:
let data = JSON.parse(e.data);
console.log(data.deviceId);
Try this
let e= { data: `{"deviceId":"1234","instanceId":"drogon","operationalEvent":"Shutdown","subEventReason":"Finished","operationalState":"in shutdown","createdAt":"2019-06-07 15:22:17","initiator":"system"}` } console.log(JSON.parse(JSON.parse(JSON.stringify(e)).data).deviceId);
JSON.parse(JSON.stringify(e)) is not non-sense as pointe above. It is used to remove object reference. You can try this
const data1 = JSON.parse(JSON.stringify(e)).data
const parsedData = JSON.parse(data1) // e.data is of type 'string' as you have pointed out
const deviceId = parsedData.deviceId
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.