主要内容:
JSON:
JSON语法规则:
利用JSON存储员工信息:
emp.json:
[
{
"empno": "7369",
"ename": "彦祖",
"job": "软件工程师",
"hiredate": "2019-05-8",
"salary": "12000",
"dname": "研发部"
},
{
"empno": "7370",
"ename": "德华",
"job": "软件测试师",
"hiredate": "2019-05-8",
"salary": "11000",
"dname": "测试部",
"customer": [
{
"cname": "李东"
},
{
"cname": "李娜"
}
]
}
]
Javascript访问JSON对象:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var json = [
{
"empno": "7369",
"ename": "彦祖",
"job": "软件工程师",
"hiredate": "2019-05-8",
"salary": "12000",
"dname": "研发部"
},
{
"empno": "7370",
"ename": "德华",
"job": "软件测试师",
"hiredate": "2019-05-8",
"salary": "11000",
"dname": "测试部",
"customers": [
{
"cname": "李东"
},
{
"cname": "李娜"
}
]
}
];
//循环遍历json
for (var i =0;i<json.length;i++){
var emp = json[i];
document.write("<h1>");
document.write(emp.empno+",");
document.write(emp.ename+",");
document.write(emp.job+",");
document.write(emp.hiredate+",");
document.write(emp.salary+",");
document.write(emp.dname+",");
document.write("</h1>");
if(emp.customers != null){
document.write("<h2>---");
for(var j=0;j<emp.customers.length;j++){
var customer = emp.customers[j];
document.write(customer.cname+",");
}
document.write("</h2>");
}
}
</script>
</head>
<body>
</body>
</html>
输出结果:
JSON与字符串相互转换:
JSON.parse():将字符串转换为JSON对象
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>字符串和json相互转换</title>
<script type="text/javascript">
var str = "{\"class_name\":\"五年三班\"}";
//将字符串转为JSON对象
var json = JSON.parse(str);
//在控制台输出str和json
console.log(str);
console.log(json);
//在页面输出json的值
document.write("班级:"+json.class_name);
</script>
</head>
<body>
</body>
</html>
输出结果:
JSON.stringify():将JSON对象转换为字符串
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON转字符串</title>
<script type="text/javascript">
var json1 = {"class_name" : "五年级四班"};
var str1 = JSON.stringify(json1);
console.info(json1);
console.info(str1);
//初始化json对象
var json2 = {};
json2.class_name = "五年级五班";
json2.floor = "逸夫楼四层";
json2.teacher = "王义夫";
console.info(json2);
</script>
</head>
<body>
</body>
</html>
输出结果:
JSON和Java交互:
将下载好的FastJson放在工程的WebContent/WEB-INF/lib目录下
FastJSON对象序列化与反序列化:
Employee.java:
package com.imooc.json;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class Employee {
private Integer empno;
private String ename;
private String job;
//注解
//@JSONField(format="yyyy-MM-dd HH:mm:ss SSS")
@JSONField(name = "hiredate" , format="yyyy-MM-dd")
private Date hdate;
private Float salary;
@JSONField(serialize = false)//表示不对其进行序列化
private String dname;
//setter getter
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHdate() {
return hdate;
}
public void setHdate(Date hdate) {
this.hdate = hdate;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
FastJsonSample1.java:
package com.imooc.json;
import java.util.Calendar;
import com.alibaba.fastjson.JSON;
public class FastJsonSample1 {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setEmpno(4488);
employee.setEname("彦祖");
employee.setJob("客户经理");
employee.setSalary(10000f);
employee.setDname("市场部");
Calendar c = Calendar.getInstance();
c.set(2019, 0, 30, 0, 0, 0);
employee.setHdate(c.getTime());
//FastJSON提供了JSON对象,来完成对象与JSON字符串的互相转换
String json = JSON.toJSONString(employee);
System.out.println(json);
//将JSON转换为对象
Employee emp = JSON.parseObject(json, Employee.class);
System.out.println(emp.getEname());
}
}
输出结果:
{"empno":4488,"ename":"彦祖","hiredate":"2019-01-30","job":"客户经理","salary":10000.0}
彦祖
FastJSON对象数组序列化与反序列化:
FastJsonSample2:
package com.imooc.json;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class FastJsonSample2 {
public static void main(String[] args) {
List emplist = new ArrayList();
for (int i = 1 ; i <= 10 ; i++) {
Employee employee = new Employee();
employee.setEmpno(4488 + i);
employee.setEname("王" + i);
emplist.add(employee);
}
//将JSON转换为字符串
String json = JSON.toJSONString(emplist);
System.out.println(json);
//将JSON转换为对象
List<Employee> emps = JSON.parseArray(json , Employee.class);
for(Employee e : emps) {
System.out.println(e.getEmpno() + ":" + e.getEname());
}
}
}
输出结果:
[{"empno":4489,"ename":"王1"},{"empno":4490,"ename":"王2"},{"empno":4491,"ename":"王3"},{"empno":4492,"ename":"王4"},{"empno":4493,"ename":"王5"},{"empno":4494,"ename":"王6"},{"empno":4495,"ename":"王7"},{"empno":4496,"ename":"王8"},{"empno":4497,"ename":"王9"},{"empno":4498,"ename":"王10"}]
4489:王1
4490:王2
4491:王3
4492:王4
4493:王5
4494:王6
4495:王7
4496:王8
4497:王9
4498:王10