4

我有以下格式的 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数据。

这个怎么做。

谢谢

4

2 回答 2

4

你需要像这样传递你的变量:

jsonData[ countryName ]

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

sobeObj.value

或者

someObj[ 'value' ]

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

于 2013-11-05T13:24:34.417 回答
0

像那样:

var countryName = 'India';
var countries = jsonData[countryName];
于 2013-11-05T13:24:47.543 回答