javascript - The string value parsed by JSON.stringify is undefined
仅有的幸福
仅有的幸福 2017-05-18 10:57:47
0
6
934

msg is an object

var descriptionMsg = JSON.stringify(msg);
descriptionMsg is printed as: {"title":"aaaaaaa","image":"xxxxxx.png","content":"vvvvvv"} is a string

But
document.write(descriptionMsg.title);or document.write(descriptionMsg['title']);

are all printed as: undefined

why is that?

仅有的幸福
仅有的幸福

reply all(6)
我想大声告诉你

descriptionMsg is already a string, so it is naturally impossible to have descriptionMsg.title;
console.log(msg.title) Try?

刘奇
document.write(JSON.parse(descriptionMsg).title)
黄舟

JSON.stringify()Used to parse a string from an object

洪涛

JSON.stringify(obj) passes in a native object and returns a string. Of course, you cannot get the value by using JSON.stringify(obj).key, so it is undefined. If you want to get the value, you can Directly use the unconverted native object obj.key, or JSON.parse(JSON.stringify(obj)).key to parse the converted json string into a native object.

阿神

descriptionMsg is a string, not an object in json format. You need to use JSON.parse to convert it. JSON.stringify converts objects into strings, but you used it the other way around.

滿天的星座

descriptionMsg is a string, so you need to convert the string into an object first, and then access the properties of the object:
document.write(JSON.parse(descriptionMsg).title)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template