开发者

How to get objects value if its name contains dots?

开发者 https://www.devze.com 2022-12-25 10:23 出处:网络
I have a very simple array (please focus on the object with "points.bean.pointsBase" as key):

I have a very simple array (please focus on the object with "points.bean.pointsBase" as key):

var mydata =   
{"list":  
  [  
    {"points.bean.pointsBase":  
      [  
        {"time开发者_StackOverflow社区": 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, it displays nothing. When I change "points.bean.pointsBase" to something without dots in its name - everything works.

However, I can't change this name to anything else without dots, but I need to get a value? Is there any options to get it?


What you want is:

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

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


in javascript, object properties can be accessed with . operator or with associative array indexing using []. ie. object.property is equivalent to object["property"]

this should do the trick

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


Try ["points.bean.pointsBase"]


If json object key/name contains dot......! like

var myJson = {"my.name":"vikas","my.age":27}

Than you can access like

myJson["my.name"]
myJson["my.age"]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号