I have json data with a colon in the label (see responsedata) which I'm finding difficult to access in Angular with the following code:
我在标签中有一个冒号的json数据(参见responsedata),我发现很难在Angular中使用以下代码访问它:
<li ng-repeat="i in items.autnresponse.responsedata | searchFor:searchString"> <p>{{i.autn:numhits}}</p> </li>
I keep getting an error like this:
我一直收到这样的错误:
Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 7 of the expression [i.autn:numhits] starting at [:numhits]
.
错误:[$ parse:syntax]语法错误:令牌':'是从[:numhits]开始的表达式[i.autn:numhits]第7列的意外标记。
JSON data excerpt:
JSON数据摘录:
"autnresponse": {
"action": {
"$": "QUERY"
},
"response": {
"$": "SUCCESS"
},
"responsedata": {
"autn:numhits": {
"$": "92"
},
"autn:totalhits": {
"$": "92"
},
"autn:totaldbdocs": {
"$": "188"
},
"autn:totaldbsecs": {
"$": "188"
},
Does anybody know a way around this?
有人知道解决这个问题吗?
2 个解决方案
#1
4
I'll assume I know the answer to my question from the comments and post what would be my response:
我会假设我从评论中知道了我的问题的答案,并发布了我的回答:
Assumption
假设
Your JSON parses fine but your code can't access something in the resulting data structure
您的JSON解析正常,但您的代码无法访问结果数据结构中的某些内容
Answer
回答
Use square bracket notation with a string:
使用带有字符串的方括号表示法:
var x = i['autn:numhits'];
The same can be used when you have a property name in a variable. Using your same example:
当您在变量中具有属性名称时,可以使用相同的名称。使用相同的示例:
var propertyName = 'autn:numhits';
var x = i[propertyName];
Addendum
附录
For Angular template, try
对于Angular模板,请尝试
{{i['autn:numhits']}}
#2
0
Use brackets to access it like a dictionary rather than dot notation. Replace {{i.autn:numhits}}
with {{i['autn:numhits']}}
使用括号可以像字典而不是点符号一样访问它。将{{i.autn:numhits}}替换为{{i ['autn:numhits']}}
As a heads up, if you want to wrap autn:numhits with double quotes you will need to html escape them.
作为一个提示,如果你想用双引号包装autn:numhits,你将需要html转义它们。
#1
4
I'll assume I know the answer to my question from the comments and post what would be my response:
我会假设我从评论中知道了我的问题的答案,并发布了我的回答:
Assumption
假设
Your JSON parses fine but your code can't access something in the resulting data structure
您的JSON解析正常,但您的代码无法访问结果数据结构中的某些内容
Answer
回答
Use square bracket notation with a string:
使用带有字符串的方括号表示法:
var x = i['autn:numhits'];
The same can be used when you have a property name in a variable. Using your same example:
当您在变量中具有属性名称时,可以使用相同的名称。使用相同的示例:
var propertyName = 'autn:numhits';
var x = i[propertyName];
Addendum
附录
For Angular template, try
对于Angular模板,请尝试
{{i['autn:numhits']}}
#2
0
Use brackets to access it like a dictionary rather than dot notation. Replace {{i.autn:numhits}}
with {{i['autn:numhits']}}
使用括号可以像字典而不是点符号一样访问它。将{{i.autn:numhits}}替换为{{i ['autn:numhits']}}
As a heads up, if you want to wrap autn:numhits with double quotes you will need to html escape them.
作为一个提示,如果你想用双引号包装autn:numhits,你将需要html转义它们。