json是一种轻量级数据交换格式,易于人阅读和编写,同时也易于机器解析和生成
json数据格式
json数组
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/**
* 定义json的数组格式
*中括号包裹,数组的元素的数据没有限制
* 元素之间逗号隔开
*/
var jsonArray = ["k1","k2",100,9.9,true]
// 遍历数组,取出数组中的元素
for (var i = 0 ; i < jsonArray.length ; i++){
console.log(jsonArray[i]);
}
</script>
</head>
<body>
</body>
</html>
json对象
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/**
* 定义json的对象格式
* 大括号包裹,定义赋值对,键必须是字符串类型,值的数据类型不限制
* 键值对之间是冒号分开的
* 每个键值对之间逗号分开
*/
var jsonObject = {"k1":"v1","k2":"v2","k3":100,"k4":9.9,"k5":true};
// 取出键值对,键找值的方式
console.log(jsonObject.k1);
console.log(jsonObject.k2);
console.log(jsonObject.k3);
console.log(jsonObject.k4);
console.log(jsonObject.k5);
</script>
</head>
<body>
</body>
</html>
json嵌套
数组套对象
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据的嵌套</title>
</head>
<body>
<script type="text/javascript">
/**
* json数组的元素是对象
* 数组定义2个元素,每个元素是对象
* 对象是键值对形式
*/
var jsonArray = [
{"name":"张三","age":20},
{"name":"里斯","age":22}
];
// 取出重要的数据,李四22
console.log( jsonArray[1].name +" == "+ jsonArray[1].age);
// 遍历数组,取出数组中的元素
for(var i=0 ; i< jsonArray.length; i++){
console.log(jsonArray[i].name + " == "+ jsonArray[i].age);
}
</script>
</body>
</html>
对象套数组
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据嵌套</title>
</head>
<body>
<script type="text/javascript">
/**
* json数据是对象,对象只是数组
*/
var jsonObject = {
"k1": ["北京,"天津","上海"],
"k2": ["中国","美国","英国"]
};
// 取出上海
console.log( jsonObject.k1[2])
// 分别取出看和可的数组,遍历
for (var i =0; i< jsonObject.k1.length; i++){
console.log(jsonObject.k1[i]);
}
console.log("===========")
for (var i =0; i< jsonObject.k2.length; i++){
console.log(jsonObject.k2[i]);
}
</script>
</body>
</html>
你中有我,我中有你
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数据嵌套</title>
</head>
<body>
<script type="text/javascript">
/**
* json的数据嵌套,你中有我,我中有你
* json的数据本质上是对象
* 对象的键是字符串
* 数组的元素是对象
*/
var json= {
// 键是K1值是数组,数组的元素是对象
"k1":[
{"name":"张三","age":20},
{"name":"里斯","age":22}
],
"k2":[
{"name":"王五","age":24},
{"name":"赵六","age":26}
]
};
// 取出数据李四26
console.log(json.k1[1].name+"==="+json.k1[1].age)
// 遍历k2键对应的数组
for(var i=0 ; i< json.k2.length; i++) {
console.log(json.k2[i].name + " == " + json.k2[i].age);
}
</script>
</body>
</html>
fastjson
他可以解析json格式的字符串,支持将Java Bean序列化为json字符串,也可以从json字符串反序列化到javaBean。
java
package src.FastJson;
import lombok.Data;
import java.util.Date;
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private String email;
private Date birthday;
}
序列化
对象转json
java
@Test
// java中的对象,Student对象,序列化为json格式字符串
public void testObjectToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setEmail("zs@www.com");
student.setBirthday(getDate());
// student对象,转到json格式字符串
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
// {"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"}
}
List转json
java
@Test
// java中的集合List,序列化为json格式字符串
public void testListToJson() {
//集合List,存储Student对象
List<Student> list = new ArrayList<Student>();
Student student1 = new Student();
student1.setId(1);
student1.setName("张三");
student1.setAge(20);
student1.setEmail("zs@www.com");
student1.setBirthday(getDate());
Student student2 = new Student();
student2.setId(2);
student2.setName("张");
student2.setAge(20);
student2.setEmail("z@www.com");
student2.setBirthday(getDate());
// Student对象存储到List集合中
list.add(student1);
list.add(student2);
// List集合序列化为json格式字符串
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
//转后的元素是数组,数组的元素是对象
// [{"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"},
// {"age":20,"birthday":0,"email":"z@www.com","id":2,"name":"张"}]
}
Map转json
java
@Test
// java中的集合Map,序列化为json格式字符串
public void testMapToJson() {
// 创建Map集合,键为字符串类型,值是Student对象
Map<String,Student> map = new HashMap<String,Student>();
Student student1 = new Student();
student1.setId(1);
student1.setName("张三");
student1.setAge(20);
student1.setEmail("zs@www.com");
student1.setBirthday(getDate());
Student student2 = new Student();
student2.setId(2);
student2.setName("张");
student2.setAge(20);
student2.setEmail("z@www.com");
student2.setBirthday(getDate());
// Map集合中存储student对象
map.put("student1", student1);
map.put("student2", student2);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
// json格式字符串是对象,对象有两个键,键对应的值是Student对象
// [{"age":20,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"},
// {"age":20,"birthday":0,"email":"z@www.com","id":2,"name":"张"}]
}
反序列化
json转对象
java
@Test
// json格式字符串,反序列化回到java对象
public void testJsonToObject() {
String jsonString = "{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"}";
// JSON类的静态方法parseObject
// 传递要反序列化的json字符串
Student student = JSON.parseObject(jsonString,Student.class);
System.out.println(student);
// Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970)
}
json转list
java
@Test
public void testJsonToList() {
String jsonString = "[{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"},{\"age\":20,\"birthday\":0,\"email\":\"z@www.com\",\"id\":2,\"name\":\"张\"}]";
// JSON类的静态方法,parseArray
// 传递json格式字符串,传递转换后的集合的泛型class对象
List<Student> list = JSON.parseArray(jsonString,Student.class);
for (Student student : list) {
System.out.println(student);
}
// Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970)
// Student(id=2, name=张, age=20, email=z@www.com, birthday=Thu Jan 01 08:00:00 CST 1970)
}
json转map
java
@Test
// JSON格式字符串反序列化到Map
public void testJsonToMap() {
String jsonString = "{\"1\":{\"age\":20,\"birthday\":0,\"email\":\"zs@www.com\",\"id\":1,\"name\":\"张三\"},\"2\":{\"age\":20,\"birthday\":0,\"email\":\"z@www.com\",\"id\":2,\"name\":\"张\"}}";
// json类的静态方法,paresObject
// 直接进行反序列化2,Map没有泛型,泛型没有是不安全的集合
// 转后的集合,必须有泛型
// 调用parseObject,传递参数,T类型,在T类的泛型,传递
Map map = JSON.parseObject(jsonString,new TypeReference<Map<String,Student>>(){});
for (Object key: map.keySet()){
System.out.println(key+":"+map.get(key));
}
// 1:Student(id=1, name=张三, age=20, email=zs@www.com, birthday=Thu Jan 01 08:00:00 CST 1970)
// 2:Student(id=2, name=张, age=20, email=z@www.com, birthday=Thu Jan 01 08:00:00 CST 1970)
}
fastjson枚举
java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
import java.util.Date;
/**
* SerializerFeature枚举:进行序列化时,可以自己定义特殊需求
* json静态方法 toJSONString()
* 方法的参数:第一个是要序列化的对象
* 方法的参数:第二个参数SerializerFeature枚举类型的可变参数
* SerializerFeature枚举的常量,做序列化的个性需求
*/
public class TestFastJson2 {
@Test
// WriteNullNumberAsZero 枚举的常量,序列化为null的字段,序列化为零
public void testWriteNullNumberAsZero(){
Student student = new Student();
student.setId(1);
student.setName("张三");
// student.setAge(20);
student.setEmail("zs@www.com");
student.setBirthday(getDate());
// 方法的参数上添加枚举类型
// 只适用于数字
String jsonString = JSON.toJSONString(student,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullNumberAsZero);
System.out.println(jsonString);
// {"age":0,"birthday":0,"email":"zs@www.com","id":1,"name":"张三"}
}
@Test
// WriteNullStringAsEmpty枚举的常量,序列化为null的字段,值序列化为""
public void testWriteNullStringAsEmpty(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
// student.setEmail("zs@www.com");
student.setBirthday(getDate());
// 方法的参数上添加枚举类型
String jsonString = JSON.toJSONString(student,SerializerFeature.WriteNullStringAsEmpty);
System.out.println(jsonString);
// {"age":20,"birthday":0,"email":"","id":1,"name":"张三"}
}
@Test
//writeMapNullValue 枚举中的常量,序列化null值的字段
public void testWriteMapNullValue(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
// student.setEmail("zs@www.com");
student.setBirthday(getDate());
// 方法的参数上添加枚举类型
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
// {"age":20,"birthday":0,"email":null,"id":1,"name":"张三"}
}
public Date getDate() {
Date date = new Date(0);
return date;
}
}
json注解
jsonfield注解
用于序列化时进行特性定制
@JSONfield注解属性name,指定序列化后的名字
ordinal,指定序列化后的字段顺序
format,指定序列化后的格式
serialize,指定是否序列化该字段
JSonType注解
注解作用于类上
includes要被序列化的字段
order要被序列化的字段的顺序
serialzeFeatures序列化时的特性定义