json和fastjson的使用

JSON

  • JavaScript Object Notation(JavaScript對象表示法)
  • JSON是輕量級的文本數據交換格式
  • JSON獨立於語言,具有自我描述性,更易理解
  • JSON用於數據的保存和傳輸

語法

  • 數據由鍵(key)/值(value)描述,由逗號分隔

  • 大括號代表一個完整的對象,其中可以擁有多個鍵/值對

  • 中括號保存數組,多個對象之間使用逗號分隔

  • 鍵值除了數字都要用雙引號括起來

//數組
[
	{
		"empno": 7891,
		"ename": "王五",
		"department": "研發部",
		"job": "java工程師",
		"salary": 15000
	},
	{
		"empno": 7892,
		"ename": "李樂",
		"department": "市場部",
		"job": "銷售經理",
		"salary": 18000,
		"customers": [
			{
				"cname": "李曉"
			},
			{
				"cname": "劉通"
			}
		]
	}
]

JavaScript訪問JSON對象

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
	/* 定義一個數組(JSON對象),採用JSON的格式 */
	/* 數組中保存的是一個個員工對象 */
	var json=[
		{
			"empno": 7891,
			"ename": "王五",
			"department": "研發部",
			"job": "java工程師",
			"salary": 15000
		},
		{
			"empno": 7892,
			"ename": "李樂",
			"department": "市場部",
			"job": "銷售經理",
			"salary": 18000,
			"customers": [
				{
					"cname": "李曉"
				},
				{
					"cname": "劉通"
				}
			]
		}
	];
	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.department);
		document.write(","+emp.job);
		document.write(","+emp.salary);
		document.write("</h1>");
		/* emp.customers也是數組(裏面保存着對象),在js中數組就是一個對象 ,判斷這個對象是否存在*/
		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>

在這裏插入圖片描述

JS中字符串和JSON互轉

將JSON格式的字符串轉換成JSON對象

JSON.parse() ,JSON是瀏覽器的內置對象

<script>
	/* 長得像JSON的字符串 引號要用轉義符*/
	var str="{\"name\":\"王勒\"}";
	/* 將字符串轉換成JSON對象 */
	var json=JSON.parse(str);
	console.log(json);
</script>

在這裏插入圖片描述
Ajax,是讓JS和java後臺進行通信的方式,java後臺服務器會返回長得像JSON的字符串,通過JSON.parse()獲得JSON對象後,在JS中才能提取其中的屬性值。

將JSON對象轉換成字符串

用JSON.stringify() 轉換成JSON格式的字符串

<script>
    /* JSON對象可以是數組對象,也可以是普通對象*/
	var json={"name":"張三"};
	var str=JSON.stringify(json);
	console.log(str);
</script>

在這裏插入圖片描述

初始化JSON對象

	var json1={};
	json1.name="張丹";
	json1.age=18;
	json1.sex="女";
	console.info(json1);

FastJSON對象的序列化和反序列化

在這裏插入圖片描述

序列化

javaBean

public class Employee {
	private int empno;
	private String ename;
	private String department;
	//fastJSON註解,因爲日期序列化後是以時間戳的形式呈現,需要用註解指定格式
	//name是定義序列化後字符串中的key值
	@JSONField(name="firedate",format="yyyy-MM-dd")
	private Date hdate;
	//讓這個屬性不被序列化
	@JSONField(serialize=false)
	private float salary;
	public int getEmpno() {
		return empno;
	}
	public void setEmpno(int empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	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;
	}
}

對象的序列化

JSON.toJSONString()

public class FastJson {
	public static void main(String[] args) {
		Employee employee=new Employee();
		employee.setEmpno(7895);
		employee.setEname("吳磊");
		employee.setDepartment("研發部");
		Calendar c=Calendar.getInstance();
		c.set(2015, 7, 21);
		employee.setHdate(c.getTime());
		employee.setSalary(15000f);
		String json=JSON.toJSONString(employee);
		System.out.println(json);
	}
}

在這裏插入圖片描述

對象的反序列化

JSON.parseObject(args1,args2) 第一個參數是json格式的字符串,第二個參數是反序列化javaBean的類

  • 如果序列化的時候某個屬性沒有被序列化,那麼反序列化的時候就爲null或者0
Employee emp=JSON.parseObject(json,Employee.class);
System.out.println(emp.getEname()+emp.getSalary());

在這裏插入圖片描述

對象數組序列化

  • 如果對象中的屬性爲空,那麼序列化的時候將自動忽略這個屬性,不體現在JSON格式的字符串中
	List<Employee> empList=new ArrayList<>();
		for(int i=0;i<3;i++) {
			Employee emp=new Employee();
			emp.setEmpno(4488+i);
			emp.setEname("員工"+i);
			empList.add(emp);
		}
		String json=JSON.toJSONString(empList);
		System.out.println(json);

在這裏插入圖片描述

對象數組的反序列化

JSON.parseArray() 第一個參數是json格式的字符串,第二個參數是反序列化數組中對象的javaBean的類,返回List<T>

		List<Employee> emps=JSON.parseArray(json,Employee.class);
		for(Employee e:emps) {
			System.out.println(e.getEmpno()+":"+e.getEname());
		}

在這裏插入圖片描述

序列化的作用

JSON.parseArray() 第一個參數是json格式的字符串,第二個參數是反序列化數組中對象的javaBean的類,返回List<T>

		List<Employee> emps=JSON.parseArray(json,Employee.class);
		for(Employee e:emps) {
			System.out.println(e.getEmpno()+":"+e.getEname());
		}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章