JSON数组格式
var josnArray=["k1","k2",100,9.9,true];
console.log(jsonArray[1]); //输出k2
for(var i=2;;i<jsonArray.length;i++){
console.log(jsonArray[i]);
}
JOSN对象格式
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);
JSON一层相互嵌套
数组中嵌套对象
var jsonArray=[
{"name":"张三","age":20},
{"name":"李四","age":22},
];
//取出第一个对象中的属性值
console.log(jsonArray[0].name+"======"+josnArray[0].age);
//遍历数组输出对象属性
for(var i=0;i<jsonArray.length;i++){
console.log(jsonArray[i].name+"======"+jsonArray[i].age);
}
对象中嵌套数组
var josnObject={
"k1":["北京","天津","上海"],
"k2":["中国","美国","英国"]
};
//取出数组中的单个元素
console.log(jsonObject.k1[2]);
//取出k1,k2数组进行遍历
for(var i=0;i<jsonObject.k1.length;i++){
console.log(josnObject.k1[i]);
}
console.log("==============");
for(var i=0;i<jsonObject.k2.length;i++){
console.log(josnObject.k2[i]);
}
JSON多层相互嵌套
var jsonObject{
"k1":[
{"name":"张三","age":"20"},
{"name":"李四","age":"22"},
];
"k1":[
{"name":"王五","age":"24"},
{"name":"赵六","age":"28"},
];
};
//取出数组中的单个属性
console.log(jsonObject.k1[1].name+"==="+jsonObject.k1[1].age);
//遍历k2对应的数组
for(var i=0;i<jsonBoject.k2.length;i++){
console.log(jsonObject.k2[i].name+"==="+jsonObject.k2[i].age);
}
FastJson
FatsJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean 序列化未JSON字符串,也可以将Json字符串反序列化到Java Bean。
FastJson的优点
- 速度快
- 使用广泛
- 测试完备
- 使用简单
-
FastJson的序列化操作
JSON.toJSONString
1、创建项目导入FastJson依赖,单元测试依赖,Lombok依赖
2、创建实体类Student@Data
public class Student {
private int id;
private String name;
private int age;
private String email;
private Date birthday;
}
对象转JSON -对象-toJSONString
结果为对象
单元测试 ```java public class StudentTest { @Test public void testJosn(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student);
System.out.println(result);
} }
- 测试结果
```java
{"age":20,"birthday":1656753454318,"email":"zs@sina.com","id":1,"name":"张三"}
List转JSON -数组-toJSONString
结果为数组,数组的元素为对象。
单元测试
@Test
public void testJosnList(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
Student student1 = new Student();
student1.setId(2);
student1.setName("李四");
student1.setAge(24);
student1.setEmail("ls@126.com");
student1.setBirthday(new Date());
ArrayList<Student> list = new ArrayList<>();
list.add(student);
list.add(student1);
String result = JSON.toJSONString(list);
System.out.println(result);
}
结果
[{"age":20,"birthday":1656754123105,"email":"zs@sina.com","id":1,"name":"张三"},{"age":24,"birthday":1656754123105,"email":"ls@126.com","id":2,"name":"李四"}]
Map转JSON -对象-toJSONString
单元测试
@Test
public void testJosnMap(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
Student student1 = new Student();
student1.setId(2);
student1.setName("李四");
student1.setAge(24);
student1.setEmail("ls@126.com");
student1.setBirthday(new Date());
Map<String,Student> map = new HashMap<>();
map.put("student",student);
map.put("student1",student1);
String result = JSON.toJSONString(map);
System.out.println(result);
}
结果
{"student":{"age":20,"birthday":1656754579419,"email":"zs@sina.com","id":1,"name":"张三"},"student1":{"age":24,"birthday":1656754579419,"email":"ls@126.com","id":2,"name":"李四"}}
FastJson的反序列化操作
JSON对象转Java对象-parseObject
JSON.parseObject(jsonString, Student.class);
单元测试
@Test
public void tsetJsonToJavaObject(){
String jsonString="{\"age\":20,\"birthday\":1656753454318,"+
"\"email\":\"zs@sina.com\",\"id\":1,\"name\":\"张三\"}";
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println(student);
}
结果
Student(id=1, name=张三, age=20, email=zs@sina.com, birthday=Sat Jul 02 17:17:34 CST 2022)
JSON的List转Java-parseArray
JSON.parseArray(jsonString, Student.class);
单元测试
@Test
public void tsetJsonToJavaArray(){
String jsonString="[{\"age\":20,\"birthday\":1656754123105,\"email\":\"zs@sina.com\"," +
"\"id\":1,\"name\":\"张三\"},{\"age\":24,\"birthday\":1656754123105,\"email\":" +
"\"ls@126.com\",\"id\":2,\"name\":\"李四\"}]";
List<Student> list = JSON.parseArray(jsonString, Student.class);
for(Student student:list){
System.out.println(student);
}
}
结果
Student(id=1, name=张三, age=20, email=zs@sina.com, birthday=Sat Jul 02 17:28:43 CST 2022)
Student(id=2, name=李四, age=24, email=ls@126.com, birthday=Sat Jul 02 17:28:43 CST 2022)
JSON的Map转Java -parseObject
JSON.parseObject(jsonString,new TypeReference
单元测试
@Test
public void tsetJsonToJavaMap(){
String jsonString="{\"student\":{\"age\":20,\"birthday\":1656754579419," +
"\"email\":\"zs@sina.com\",\"id\":1,\"name\":\"张三\"},\"student1\"" +
":{\"age\":24,\"birthday\":1656754579419,\"email\":\"ls@126.com\"," +
"\"id\":2,\"name\":\"李四\"}}";
Map<String, Student> map = JSON.parseObject(jsonString,
new TypeReference<Map<String, Student>>() {});
for (String key:map.keySet()){
System.out.println(key+"::"+map.get(key));
}
}
结果
student::Student(id=1, name=张三, age=20, email=zs@sina.com, birthday=Sat Jul 02 17:36:19 CST 2022)
student1::Student(id=2, name=李四, age=24, email=ls@126.com, birthday=Sat Jul 02 17:36:19 CST 2022)
SerializerFeature序列化对象
WriteMapNullValue
没有赋值的字符串属性,序列化时不会显示该属性,在序列化时加上该参数,序列化时该属性会显示为null
单元测试
@Test
public void testNull(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
// String result = JSON.toJSONString(student);
System.out.println(result);
}
结果
{"age":0,"birthday":1657007508874,"email":"zs@sina.com","id":1,"name":null}
WriteNullStringAsEmpty
将没有赋值的属性,显示为空字符串。
单元测试
@Test
public void testNullToEmpty(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty);
System.out.println(result);
}
结果
{"age":0,"birthday":1657007677593,"email":"zs@sina.com","id":1,"name":""}
WriteNullNumberAsZero
将没有赋值的数值型的属性显示为0(注:版本默认将不赋值的数值型属性显示为0),该属性不会影响不赋值的字符串属性是否显示。
单元测试
@Test
public void testNullToZero(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
System.out.println(result);
}
结果
@Test
public void testNullToZero(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
System.out.println(result);
}
WriteNullBooleanAsFalse
将未赋值的Boolean值序列化为false。
单元测试
@Test
public void testBooleanToFalse(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
// student.setFlag(true);
String result = JSON.toJSONString(student, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println(result);
}
结果
{"age":0,"birthday":1657008348277,"email":"zs@sina.com","flag":false,"id":1}
WriteDateUseDateFormat
将时间日期序列化为标准的格式。
单元测试
@Test
public void testDateToFormat(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat);
System.out.println(result);
}
结果
{"age":0,"birthday":"2022-07-05 16:09:20","email":"zs@sina.com","id":1}
PrettyFormat
将输出的格式转化为纵向输出。
单元测试
@Test
public void testDateToPrettyFormat(){
Student student=new Student();
student.setId(1);
// student.setName("张三");
// student.setAge(20);
student.setEmail("zs@sina.com");
student.setBirthday(new Date());
String result = JSON.toJSONString(student, SerializerFeature.PrettyFormat);
System.out.println(result);
}
结果
{
"age":0,
"birthday":1657008638537,
"email":"zs@sina.com",
"id":1
}
JSonFields注解
该注解可以使用在类上,方法上和参数上。
name属性,可以表明序列化后的字段名
- ordinal属性,表明系列化后的字段的顺序,默认值为0,值越大越往后
- format属性,表明序列化后的格式
- serialize属性,是否序列化
- disserialize属性,是否反序列化(注:未序列化的不可反序列化)
serialzeFeatures序列化时的特性定义。与SerializerFeature类似。
@JSonFields(name="studentName",ordinal=1)
@JSonFields(format="YYYY-MM-dd")
@JSonFields(serialize="false")
JSonType注解
该注解作用在类上
includes属性,指明需要进行序列化的属性(注:与JSonFields注解冲突时,JSonType优先)
- orders属性,指明序列化后的顺序。
- serialzeFeatures属性,与SerializerFeature类似
@JSonType(includes={"id","name","address"},orders={"name","address","age"})