Accessing JSON data with colon in the label

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?

Answers:

Answer

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

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']}}
Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.