如何在javascript中将字符串转换为符号

时间:2013-11-05 13:23:03

标签: javascript json

我有以下格式的json数据。

 var jsonData={
"India":["Australia","New Zealand","Pakistan","England"],
"Pakistan":["New Zealand","India","England"],
"Bangladesh":["New Zealand","India","England","SouthAfrica","Australia"],
"Srilanka":["New Zealand","India","England","SouthAfrica","Australia"],
"Australia":["New Zealand","Pakistan","India"],
"New Zealand":["Pakistan","India","England","SouthAfrica","Australia"],
"SouthAfrica":["Srilanka","India","Pakistan"],
"England":["Srilanka","New Zealand","Pakistan","Bangladesh"]
 };

我将从下拉列表中获取国家/地区名称。用户已选择。

  var countryName=document.getElementById("pop1").value;

现在我想根据countryName值从json数据中获取特定的国家/地区数据。

例如。如果countryName值等于India,那么它应该返回jsonData.India数据。

如何做到这一点。

由于

2 个答案:

答案 0 :(得分:4)

您需要像这样传递变量:

jsonData[ countryName ]

所以基本上你可以通过两种方式访问​​JS中的对象属性:

sobeObj.value

someObj[ 'value' ]

括号内可以是表示字符串的字符串或变量。

答案 1 :(得分:0)

就像那样:

var countryName = 'India';
var countries = jsonData[countryName];