Access JSON data with colon in shortcut

I have json data with a colon in the label (see answers), which is hard for me to get in Angular with the following code:

<li ng-repeat="i in items.autnresponse.responsedata | searchFor:searchString"> <p>{{i.autn:numhits}}</p> </li>

I get this error:

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 anyone know about this?

+4
source share
2 answers

I assume that I know the answer to my question from comments and posts, which will be my answer:

Assumption

Your JSON is well understood, but your code cannot access anything in the resulting data structure.

Answer

Use square notation with a string:

var x = i['autn:numhits'];

The same can be used when you have a property name in a variable. Using the same example:

var propertyName = 'autn:numhits';
var x = i[propertyName];

Adding

Angular

{{i['autn:numhits']}}
+4

, , . {{i.autn:numhits}} {{i['autn:numhits']}}

-, autn: numhits , html .

0

Source: https://habr.com/ru/post/1546866/


All Articles