我有一个名为“message”的对象
“message”包含一个匿名对象(作为字符串):
{"action":"wakeup","hello":"testing123"}
// this is what I get when I output "message" with alert()
我如何处理/从中获取“hello”的内容?
最佳答案
如果 message
是一个字符串(使用 typeof message === 'string'
检查它),您可以使用 message = JSON 从它创建一个对象.parse(消息)
。之后,您将在 message
中获得 message.action
(值“wakeup”)和 message.hello
(值“testing123”),现在是一个对象。
如果 message
已经是一个对象,则 message.wakeup
和 message.hello
应该无需转换即可使用。
关于javascript - 如何从对象内部的对象获取/寻址元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10653604/