Javascript json 字段名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5716792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json fieldnames spaces
提问by Lopoc
I've such a json structure:
我有这样一个json结构:
info:
{
First Name: "Robert",
Last Name: "Smith"
}
I'm tring to point to data with javascript using something like: "info.First Name" I know it's incorrect. How can I retrieve those information from the structure I have?
我正在尝试使用类似以下内容的 javascript 指向数据:“info.First Name” 我知道这是不正确的。如何从我拥有的结构中检索这些信息?
thank
谢谢
回答by Andy E
That's not valid JSON. JSON is a data transport format that requires field names to be string delimited with double quotes, e.g.
这不是有效的 JSON。JSON 是一种数据传输格式,它要求字段名称以字符串分隔并用双引号分隔,例如
{
"info" : {
"First Name": "Robert",
"Last Name": "Smith"
}
}
After parsing, you can then use obj.info["First Name"]
to access the First Name field.
解析后,您可以使用obj.info["First Name"]
访问 First Name 字段。
What you have is a JS object literal (that's still invalid), but you can apply the same technique (stringify the property names) to reach the same end goal.
您拥有的是 JS 对象文字(仍然无效),但您可以应用相同的技术(将属性名称字符串化)来达到相同的最终目标。