XML和JSON数据的解析
<!DOCTYPE html>
<!-- 这是HTML的注释 -->
<html lang="en" id="myHtml">
<head>
<!-- 这里不是设置了编码,而是告诉浏览器,用什么编码方式打开文件避免乱码 -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HtmlAll</title>
</head>
<body>
<!-- JSON全称为JavaScript Object Notation即为数据交换格式,对象标记 -->
<!-- JSON的主要作用是一种标准的轻量的数据交换格式,特点是体积小易解析 -->
<!-- 在实际的开发中,JSON和XML是用的最多的数据交换格式 -->
<!-- XML体积大,解析麻烦,优点是格式严谨,相对更难解析 -->
<!-- HTML和XML有一个父类叫SGML(标准通用的标记语言) -->
<script type="text/javascript">
window.onload = function()
{
var jsonObj =
{
"sno" : "110",
"sname" : "Jack",
"sex" : "男",
};
student = function(sno,sname,sex)
{
this.sno = sno;
this.sname = sname;
this.sex = sex;
}
//访问json对象属性
// Json也被称为无类型对象,轻量级,轻巧,体积小,易解析
// alert(jsonObj.sno + jsonObj.sname + jsonObj.sex);
var stu = new student(jsonObj.sno,jsonObj.sname,jsonObj.sex);
// alert(stu.sno + stu.sname + stu.sex);
var students =
[
{
"sno" : "110",
"sname" : "Jack",
"sex" : "男",
},
{
"sno" : "120",
"sname" : "Rose",
"sex" : "女",
},
{
"sno" : "130",
"sname" : "Tim",
"sex" : "男",
}
];
for (var i = 0; i < students.length; i++)
{
var student = students[i];
// alert(student.sno + student.sname + student.sex);
}
var user =
{
"usercode" : 110,
"username" : "Rose",
"sex" : true,
"address" : {
"city" : "北京",
"street" : "大兴区",
"zipcode" : "12221231"
},
"aihao" : ["smoke","drink","tt"]
};
// 访问人名以及居住的城市
// alert(user.username + user.address.city);
//描述整个班级
var iop =
{
"total" : 3,
"studentInfo" : [
{
"usercode" : 110,
"username" : "Rose",
"sex" : true,
"address" : {
"city" : "北京",
"street" : "大兴区",
"zipcode" : "12221231"
},
"aihao" : ["smoke","drink","tt"]
},
{
"usercode" : 110,
"username" : "Jack",
"sex" : true,
"address" : {
"city" : "北京",
"street" : "大兴区",
"zipcode" : "12221231"
},
"aihao" : ["smoke","drink","tt"]
},
{
"usercode" : 110,
"username" : "Tim",
"sex" : true,
"address" : {
"city" : "北京",
"street" : "大兴区",
"zipcode" : "12221231"
},
"aihao" : ["smoke","drink","tt"]
}
]
}
//遍历学生表所有的学生名字
for (var i = 0; i < iop.studentInfo.length; i++) {
alert(iop.studentInfo[i].username);
}
}
</script>
</body>
</html>
xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<sname>1</sname>
<sex>1</sex>
</student>
<student>
<sname>2</sname>
<sex>1</sex>
</student>
<student>
<sname>3</sname>
<sex>1</sex>
</student>
<student>
<sname>4</sname>
<sex>1</sex>
</student>
</students>
标签:XML,jsonObj,sname,sno,JavaScript,sex,JSON,110,var
From: https://blog.51cto.com/u_16322355/8329198