Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON with spaces in key name with AngularJS

I have a json which has spaces in key names.

The JSON has the following format

    {
     "response":{"docs":[
        {
          "my name":"krammer",
          "job": "stackexchange"
        }
                }
    }

While using ng-repeat to get the parameters into a list, I use the following code

{{friends.['my name']}}

But this gives an empty output. While

friends.my name

gives an error.

So how can the key names with empty spaces be accessed using AngularJS ?

like image 347
krammer Avatar asked Feb 26 '14 11:02

krammer


2 Answers

Please try this

{{friends['my name']}}
like image 56
SajithNair Avatar answered Nov 08 '22 23:11

SajithNair


It doesn't have anything to do with angular, this is the way we read props from JavaScript object, here you have a object called friends. so these are all we can do, if we don't have invalid javascript characters for naming in JavaScript, like space and some other:

friends.myname
friends['myname']
friends["myname"]

but when we have those invalid characters we only can do:

friends['my name']
friends["my name"]
like image 43
Mehran Hatami Avatar answered Nov 08 '22 22:11

Mehran Hatami