admin管理员组文章数量:1387360
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:
<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]
.
JSON data excerpt:
"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?
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:
<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]
.
JSON data excerpt:
"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?
Share Improve this question edited Jan 19, 2023 at 16:40 aynber 23k9 gold badges54 silver badges68 bronze badges asked Jul 2, 2014 at 13:53 tercou1tercou1 1412 silver badges13 bronze badges 6- Which language are you trying to access it with? JavaScript? Perl? Python? Something else? – cnsumner Commented Jul 2, 2014 at 13:57
- apologies, trying to access it with angularjs – tercou1 Commented Jul 2, 2014 at 14:00
- Is it failing to parse from JSON into JS structure, or is a line of code you wrote having trouble accessing something in the resulting JS structures? If the latter, please provide the line of code. – JAAulde Commented Jul 2, 2014 at 14:04
- it works fine for the labels that have no colon in the label but when I try something like {{i.autn:numhits}} i get the error... it doesn't like the colon in the json label – tercou1 Commented Jul 2, 2014 at 14:10
- <li ng-repeat="i in items.autnresponse.responsedata | searchFor:searchString"> <p>{{i.autn:numhits}}</p> </li> – tercou1 Commented Jul 2, 2014 at 14:12
2 Answers
Reset to default 5I'll assume I know the answer to my question from the ments and post what would be my response:
Assumption
Your JSON parses fine but your code can't access something in the resulting data structure
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
{{i['autn:numhits']}}
Use brackets to access it like a dictionary rather than dot notation. Replace {{i.autn:numhits}}
with {{i['autn:numhits']}}
As a heads up, if you want to wrap autn:numhits with double quotes you will need to html escape them.
本文标签: javascriptAccessing JSON data with colon in the labelStack Overflow
版权声明:本文标题:javascript - Accessing JSON data with colon in the label - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744512723a2609978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论