我有以下格式的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
数据。
如何做到这一点。
由于
答案 0 :(得分:4)
您需要像这样传递变量:
jsonData[ countryName ]
所以基本上你可以通过两种方式访问JS中的对象属性:
sobeObj.value
或
someObj[ 'value' ]
括号内可以是表示字符串的字符串或变量。
答案 1 :(得分:0)
就像那样:
var countryName = 'India';
var countries = jsonData[countryName];