javascript 使用 AngularJS 解析键名中带有空格的 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22039760/
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
Parsing JSON with spaces in key name with AngularJS
提问by krammer
I have a json which has spaces in key names.
我有一个 json,它的键名中有空格。
The JSON has the following format
JSON 具有以下格式
{
"response":{"docs":[
{
"my name":"krammer",
"job": "stackexchange"
}
}
}
While using ng-repeat to get the parameters into a list, I use the following code
在使用 ng-repeat 将参数放入列表时,我使用以下代码
{{friends.['my name']}}
But this gives an empty output. While
但这给出了一个空的输出。尽管
friends.my name
gives an error.
给出一个错误。
So how can the key names with empty spaces be accessed using AngularJS ?
那么如何使用 AngularJS 访问带有空格的键名呢?
回答by SajithNair
Please try this
请试试这个
{{friends['my name']}}
回答by Mehran Hatami
It doesn't have anything to do with angular, this is the way we read props from JavaScript object, here you have a object called friends
. so these are all we can do, if we don't have invalid javascript characters for naming in JavaScript, like space and some other:
它与 angular 没有任何关系,这是我们从 JavaScript 对象中读取 props 的方式,这里有一个名为friends
. 所以这些就是我们所能做的,如果我们在 JavaScript 中没有用于命名的无效 javascript 字符,比如空格和其他一些:
friends.myname
friends['myname']
friends["myname"]
but when we have those invalid characters we only can do:
但是当我们有那些无效字符时,我们只能这样做:
friends['my name']
friends["my name"]
回答by Eric
You may run into a case where {{friends['my name']}} does not work. If so, try the following:
您可能会遇到 {{friends['my name']}} 不起作用的情况。如果是这样,请尝试以下操作:
{{friends.my_name}}
回答by Preethika
{{friends['my name']}}
will be working fine
{{friends['my name']}}
会工作正常