<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
var smsTypeDesc = {"4":"回訪短信","3":"郵件短信","aa":"測試短信"};
function EnumaKey(){
for(var key in smsTypeDesc){
alert(key);
}
}
function GetVal(){
var key = prompt("請輸入要查詢的key","4");
if("undefined"==typeof(smsTypeDesc)) return;
if("undefined"==typeof(smsTypeDesc[key])){
alert("輸入的key:"+key+", 在json對象中不存在!");
return;
}
alert("您輸入的key是:"+key + ",該key所對應的值是:"+smsTypeDesc[key]);
}
function GetValByKey(){
alert(smsTypeDesc.aa);
}
</script>
</head>
<body>
Json對象:<br/>
<pre>
var smsTypeDesc = {"4":"回訪短信","3":"郵件短信","aa":"測試短信"};
</pre>
<input type="button" onclick="EnumaKey();" value="遍歷smsTypeDesc所有key"/>
<input type="button" onclick="GetVal();" value="獲取smsTypeDesc動態指定key的值"/>
<input type="button" onclick="GetValByKey();" value="獲取smsTypeDesc key aa 所對應的值"/>
</body>
</html>
通過上面的代碼,可以看出很簡單。根據動態key,主要是通過[] 訪問,對於知道key的通過.訪問