<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
function test() {
var jsontext = '{"man":{"weight":"75kg", "age":"24"},"ar":["1", "2", "3", "4"]}';
//解析json
var aman = eval("("+jsontext+")");
//取出json中的信息
alert(aman.man.weight);
}
function showJSON() {
var user = {
"username":"andy",
"age":"24",
"info":{"tel":"123456", "cellphone":"98756"},
"address":[
{"city":"beijing", "postcode":"222233"},
{"city":"newwork", "postcode":"444455"},
]
}
user.username = "tom";
alert("username="+user.username);
alert("age="+user.age);
alert("info="+user.info.cellphone);
alert("address第一个地址 =" + user.address[0].city);
alert("address第二个地址 =" + user.address[1].city);
}
function showCar() {
var carr = new Car("Donge", "Coronet R/T", 123, "yellow");
alert(carr.toJSONString());
}
function Car(make, model, year, color) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
}
function myEval() {
var str = '{ "name": "Violet", "occupation": "character" }';
var obj = eval('(' + str + ')');
alert(obj.toJSONString());
}
function myEval2() {
var str = '{ "name": "Violet2", "occupation": "character2" }';
var obj = str.parseJSON();
alert(obj.toJSONString());
}
function toJsonString() {
var continents = new Array();
continents.push("Europe");
continents.push("Asia");
continents.push("Australia");
continents.push("Antarctica");
continents.push("North America");
continents.push("South America");
continents.push("Africa");
alert("The JSON representation of the continents array is: " +continents.toJSONString());
}
</script>
</head>
<body>
<input type="button" value="测试JSON" οnclick="test();"><br />
<input type="button" value="测试JSON2" οnclick="showJSON();"><br />
<input type="button" value="showCar" οnclick="showCar();"><br />
<input type="button" value="Eval转换" οnclick="myEval();"><br />
<input type="button" value="JSON转换" οnclick="myEval2();"><br />
<input type="button" value="toJsonString" οnclick="toJsonString();"><br />
</body>
</html>