[英]How do I parse and access JSON in JS?
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. JSON.parse(JSON.stringify(e))
是无意义的,它与最初的原始对象e
相同。 Since you say that e.data
is a string, that's what you need to parse: 因为您说
e.data
是一个字符串,所以您需要解析以下内容:
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. JSON.parse(JSON.stringify(e))并非像上面提到的那样无意义。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.