1. JSON 的 写法 和 简介
原创
©著作权归作者所有:来自51CTO博客作者咸咸瑜瑜的原创作品,请联系作者获取转载授权,否则将追究法律责任
JSON 很常用啊 也很有用,反正比XML好又快
json简介
json的全称是JavaScript Object Notation,是一种数据交换格式,独立于编程语言,可以用来存储数据,优点是非常利于人类阅读和编写,同时也利于机器的解析和生成,同时还可以提升网络传输效率。
json的数据格式是以名-值对的方式存储的,可以将其看做是一个字符串,下面就是一个json格式的数据,对象会放到{}中,数组放到[]中:
两种写法:(JAVASCRIPT 中 写法)
<script>
let persn = {
name:'bihu',
age:18,
sex:1,
birthday:'2000-01-01'
};
console.log(persn.name); //直接输出属性 也可以这样输出:
console.log(persn['age']); //很少用 麻烦死了...
/*
上面对象 下面数组
*/
let animal = [
{
name:'monkey',
count:10
},
{
name:'dog',
count: 20
}
]
console.log(animal[0].name); //数组的话 要先获取到对象元素 然后获取到对象元素的属性即可.
</script>
在json中规定的字符编码时utf-8,几乎所有的编程语言都有解析json数据的第三方库,
因此json的应用非常多。在servlet中可以将要返回给前台的数据封装为json格式,
在前台中可以使用javascript来解析,javascript已经内置了json的解析。
json格式的数据是跟语言无关的,在java中也有非常多的第三方库可以解析json格式的数据,常用的有:
- fastjson:国人开发的,是目前java语言中最快的json库
- gson:google开发
- jackson:Tatu Saloranta开发
下面以fastjson为例看下,自己去百度下载啊
fastjson中常用的方法:
- JSON.toJSONString将JavaBean转成json格式的字符串
- JSON.parseObject将json格式的字符串转换为JavaBean
代码演示:
我们创建一个学生类: GET 和 SET 都要有 再来个构造啊【规范点请加空构造 养成好习惯 这里忘记了 随便啦】
package temp;
import java.util.Date;
public class student {
private String name;
private int age;
private char sex;
private Date birthday;
public student1(String name, int age, char sex, Date birthday) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
再来个学校类:
package temp;
import java.util.List;
public class school {
private List<student> zspt;
public school(List<student> zspt) {
super();
this.zspt = zspt;
}
public school() {
}
public List<student> getZspt() {
return zspt;
}
public void setZspt(List<student> zspt) {
this.zspt = zspt;
}
}
其次我们尝试用插件去生成JSON 和 解析JSON:
<%@page import="java.text.DateFormat"%>
<%@page import="com.alibaba.fastjson.JSON"%>
<%@page import="temp.school"%>
<%@page import="temp.student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
student bihu = new student("bihu",18,'男',DateFormat.getDateInstance().parse("2020-01-01"));
student zhangsan = new student("zhangsan",21,'女',DateFormat.getDateInstance().parse("1999-12-03"));
school zspt = new school();
zspt.getZspt().add(bihu);
zspt.getZspt().add(zhangsan);
//开始生成 JSON :
String strJSON = JSON.toJSONString(zspt);
//JSON 转对象:
school objJSON = (school)JSON.parseObject(strJSON, school.class);
//然后分别装入 reques域中 在网页打印:
request.setAttribute("json_str",strJSON );
request.setAttribute("json_obj", objJSON);
//还可以打印: 都没问题的!
System.out.print(objJSON.getZspt().get(0).getName());
System.out.print(objJSON.getZspt().get(0).getBirthday());
%>
json_str : ${ json_str }
<br>
json_obj : ${ json_obj }
</body>
</html>
单层JSON 和 多层JSON 对比:
{"age":18,"birthday":1577808000000,"name":"bihu","sex":"男"}
----------------------------------------------------------------
{"zspt":
[
{"age":18,"birthday":1577808000000,"name":"bihu","sex":"男"},
{"age":21,"birthday":944150400000,"name":"zhangsan","sex":"女"}
]
}
其实不喜欢这种方式解析 JAVA有自带的
作者:咸瑜