JSON全称: (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。
有什么用?就是给Javascript解析数据的,一种约定成俗的传输规范。
一个简单的例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test JSON</title>
<script language="javascript" type="text/javascript">
function showJSON() {
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="showJSON()" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test JSON</title>
<script language="javascript" type="text/javascript">
function showJSON() {
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="showJSON()" />
</body>
</html>
运行点击button就看到效果了。
参考文献:
http://blog.ixpub.net/html/07/12399407-251523.html