json键作为数字 [英] json keys as numbers

查看:77
本文介绍了json键作为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传递给脚本的JSON。
我不知道JSON键,因为它们是动态的。

I have a JSON passed to script. I do not know JSON keys as they are dynamic.

实际上,它们是数字。
这就是我得到的。

Actually, they are numbers. That's what I'm getting.


var countries = {"223":"142,143","222":"23,26,25,24","170":"1,2"};

我试图访问这样的数据:

I tried to access data like this:


var objKey = 223;  (var objKey = "223";)
countries.objKey;

我尝试将JSON更改为

I tried changing JSON to


var countries = {"country223":"142,143","country222":"23,26,25,24","country170":"1,2"};

...并按如下方式访问:

... and access it like this:


var objKey = "country"+223; (var objKey = "country"+"223";)
countries.objKey;

...再没有。

任何建议都将不胜感激。

Any advice would be greatly appreciated.

推荐答案

而不是:

countries.objKey;

这样做:

 countries[objKey];

使用方括号表示法,您可以使用变量中引用的值(或使用字符串或数字) )引用属性名称。

With square bracket notation, you can use the value referenced in a variable (or use a string or number) to reference the property name.

这篇关于json键作为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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