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?
descriptionMsg is already a string, so it is naturally impossible to have descriptionMsg.title;
console.log(msg.title) Try?
JSON.stringify()
Used to parse a string from an objectJSON.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)