如果名称包含点,如何获取 JSON 对象值? [英] How to get JSON objects value if its name contains dots?

查看:30
本文介绍了如果名称包含点,如何获取 JSON 对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的 JSON 数组(请关注 "points.bean.pointsBase" 对象):

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

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

但是,不幸的是,它没有显示任何内容.
当我将 "points.bean.pointsBase" 更改为名称中没有点的 smth 时 - 一切正常!

但是,我不能将此名称更改为没有点的任何其他名称,但我需要获取一个值?!
有什么办法可以得到吗?

But, unfortunately, it does display nothing.
When I change "points.bean.pointsBase" to smth without dots in it's 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?

推荐答案

你想要的是:

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

在 JavaScript 中,您可以使用 .运算符,您可以使用 [] 和字段名称的字符串版本进行访问.

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

这篇关于如果名称包含点,如何获取 JSON 对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆