<html>
<head>
<title>JSON——exercise</title>
</head>
<body align="center">
<script type="text/javascript">
function jsonObject()
{
var myJSONtext =
{
"bindings":
[
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
//var str = '{ "name": "Violet", "occupation": "character" }';
var myObject = eval(myJSONtext);
alert("对象长度:"+myObject.bindings.length);
for(var i=0;i<myObject.bindings.length;i++){
alert(myObject+'.bindings['+i+'].method:'+myObject.bindings[i].method);
}
}
function jsonArray(){
var myJsonArray='{"name":"ghc","age":"12","sex":"男"}';
var str1=eval('('+myJsonArray+')');
var str2=eval('({"name":"ghc","age":"12","sex":"男"})');
alert(myJsonArray+".name是:"+str1.name);
alert(str2+".name是:"+str2.name);
}
</script>
<input type="button" onclick="jsonObject()" value="JSON对象" /><br/><hr>
<input type="button" onclick="jsonArray()" value="JSON字符串" />
</body>
</html>
posted on 2013-04-11 10:50
宏才 阅读(160)
评论(0) 编辑 收藏 所属分类:
JSON