Json的使用和FastJSON的使用

利用Json保存员工信息

创建emp.json文件

[
	{
		"empno": 110,
		"ename": "李白",
		"job": "软件工程师",
		"hiredate": "2021-01-12",
		"salary": 13000,
		"dname": "开发部"
	},
	{
		"empno": 111,
		"ename": "王维",
		"job": "客户经理",
		"hiredate": "2020-11-23",
		"salary": 23000,
		"dname": "市场部",
		"customers": [
			{
				"cname": "小白"
			},
			{
				"cname": "小明"
			},
			{
				"cname": "jack"
			}
		]
	},
	{
		"empno": 111,
		"ename": "杜甫",
		"job": "项目经理",
		"hiredate": "2000-10-13",
		"salary": 30000,
		"dname": "市场部"
	}
]

使用js访问json数据

sample.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	var json = [ {
		"empno" : 110,
		"ename" : "李白",
		"job" : "软件工程师",
		"hiredate" : "2021-01-12",
		"salary" : 13000,
		"dname" : "开发部"
	}, {
		"empno" : 111,
		"ename" : "王维",
		"job" : "客户经理",
		"hiredate" : "2020-11-23",
		"salary" : 23000,
		"dname" : "市场部",
		"customers" : [ {
			"cname" : "小白"
		}, {
			"cname" : "小明"
		}, {
			"cname" : "jack"
		} ]
	}, {
		"empno" : 111,
		"ename" : "杜甫",
		"job" : "项目经理",
		"hiredate" : "2000-10-13",
		"salary" : 30000,
		"dname" : "市场部"
	} ];
	//控制台输出
	console.log(json);
	for(var i=0;i<json.length;i++){
		var emp=json[i];
		document.write("<h1>");
		document.write("序号:"+(i+1));
		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 cus=emp.customers[j];
					document.write(cus.cname+"  ");
				}
			document.write("</h2>");
		}
	}
	
</script>
</head>
<body>

</body>
</html>

Json的使用和FastJSON的使用

Json与字符串对象互相转换

  • JSON.parse()方法将字符串转换为JSON对象
<script type="text/javascript">
	var str="{\"class_name\":\"软件工程1班\"}"; //字符串
	var json=JSON.parse(str);   //字符串转json对象
	console.log(json);
	console.log(str);
	document.write("班级:"+json.class_name);
</script>
  • JSON.stringify()方法将JSON对象转换为字符串
<script type="text/javascript">
	var json1={"class_name":"计算机科学2班"};  //json对象转字符串
	var str1=JSON.stringify(json1);
	console.log(json1);
	console.log(str1);
</script>

FastJSON的使用

java的JSON的工具包,把java对象转换成json对象

下载https://github.com/alibaba/fastjson/wiki/Quick-Start-CN

Json的使用和FastJSON的使用

对象序列化与反序列化

Employee.java

package com.ulikeme.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")
	private Date hdate;
	private Float salary;
	/**
	 * 此注解设置不对dname序列化
	 */
	@JSONField(serialize=false)
	private String dname;
	public 	Employee(Integer empno, String ename, String job, Date hdate, Float salary, String dname) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.hdate = hdate;
		this.salary = salary;
		this.dname = dname;
	}
	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.ulikeme.json;

import java.util.Calendar;

import com.alibaba.fastjson.JSON;

public class FastJsonSample1 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		//设置时间
		Calendar c=Calendar.getInstance();
		c.set(2018, 00, 20);
		Employee employee=new Employee(110, "李白", "程序员",c.getTime() ,10000f, "开发部");
		//FastJson中提供了Json对象,完成对象与json对象的互相转换
		String json= JSON.toJSONString(employee);
		System.out.println(json);
		System.out.println("---------------------");
		//将json转换成对象
		 Employee emp= JSON.parseObject(json, Employee.class);
		 System.out.println(emp.getEname());
		
	}

}

对象数组序列化与反序列化


public class FastJsonSample2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		List emplist=new ArrayList();
		for(int i=1;i<=10;i++) {
			Employee employee=new Employee();
			employee.setEmpno(100+i);
			employee.setEname("员工"+i);
			emplist.add(employee);
		}
		//可以把数组,list,set进行json格式化输出
		System.out.println();
		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.getEname()+":"+e.getEmpno());
		}
	}

}

 

 

上一篇:ArcGIS API for JavaScript 4.2学习笔记[1] 显示地图


下一篇:MyBatis.1入门篇