How to get the value of JSON objects if its name contains dots?

I have a very simple JSON array (please focus on "points.bean.pointsBase" ):

var mydata = {"list": [ {"points.bean.pointsBase": [ {"time": 2000, "caption":"caption text", duration: 5000}, {"time": 6000, "caption":"caption text", duration: 3000} ] } ] }; // Usually we make smth like this to get the value: var smth = mydata.list[0].points.bean.pointsBase[0].time; alert(smth); // should display 2000 

But, unfortunately, he does not show anything.
When I change "points.bean.pointsBase" to the fact that without dots in the name - everything works!

However, I cannot change this name to anything else without dots, but do I need to get the value ?!
Are there any options for getting it?

+62
json javascript object arrays
Apr 05 '10 at 6:21
source share
4 answers

What would you like:

 var smth = mydata.list[0]["points.bean.pointsBase"][0].time; 

In JavaScript, any field that you can access using. operator, you can access using [] with the string version of the field name.

+138
Apr 05 '10 at 6:24
source share

in javascript, access to object properties. operator or with associative indexing of an array using []. i.e. object.property equivalent to object["property"]

this should do the trick

 var smth = mydata.list[0]["points.bean.pointsBase"][0].time; 
+19
Apr 05 '10 at 6:29
source share

Try ["points.bean.pointsBase"]

+10
Apr 05 '10 at 6:25
source share

To use the updated solution, try using the lodash utility https://lodash.com/docs#get

+1
Apr 12 '16 at 8:48
source share



All Articles