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
 |  Show 1 more ment

2 Answers 2

Reset to default 5

I'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