JSON数组格式

  1. var josnArray=["k1","k2",100,9.9,true];
  2. console.log(jsonArray[1]); //输出k2
  3. for(var i=2;;i<jsonArray.length;i++){
  4. console.log(jsonArray[i]);
  5. }

JOSN对象格式

  1. var jsonObject={"k1":"v1","k2":"v2","k3":100,"k4":9.9,"k5":true}
  2. console.log(jsonObject.k1);
  3. console.log(jsonObject.k2);
  4. console.log(jsonObject.k3);
  5. console.log(jsonObject.k4);
  6. console.log(jsonObject.k5);

JSON一层相互嵌套

数组中嵌套对象

  1. var jsonArray=[
  2. {"name":"张三","age":20},
  3. {"name":"李四","age":22},
  4. ];
  5. //取出第一个对象中的属性值
  6. console.log(jsonArray[0].name+"======"+josnArray[0].age);
  7. //遍历数组输出对象属性
  8. for(var i=0;i<jsonArray.length;i++){
  9. console.log(jsonArray[i].name+"======"+jsonArray[i].age);
  10. }

对象中嵌套数组

  1. var josnObject={
  2. "k1":["北京","天津","上海"],
  3. "k2":["中国","美国","英国"]
  4. };
  5. //取出数组中的单个元素
  6. console.log(jsonObject.k1[2]);
  7. //取出k1k2数组进行遍历
  8. for(var i=0;i<jsonObject.k1.length;i++){
  9. console.log(josnObject.k1[i]);
  10. }
  11. console.log("==============");
  12. for(var i=0;i<jsonObject.k2.length;i++){
  13. console.log(josnObject.k2[i]);
  14. }

JSON多层相互嵌套

  1. var jsonObject{
  2. "k1":[
  3. {"name":"张三","age":"20"},
  4. {"name":"李四","age":"22"},
  5. ];
  6. "k1":[
  7. {"name":"王五","age":"24"},
  8. {"name":"赵六","age":"28"},
  9. ];
  10. };
  11. //取出数组中的单个属性
  12. console.log(jsonObject.k1[1].name+"==="+jsonObject.k1[1].age);
  13. //遍历k2对应的数组
  14. for(var i=0;i<jsonBoject.k2.length;i++){
  15. console.log(jsonObject.k2[i].name+"==="+jsonObject.k2[i].age);
  16. }

FastJson

FatsJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean 序列化未JSON字符串,也可以将Json字符串反序列化到Java Bean。
FastJson的优点

  • 速度快
  • 使用广泛
  • 测试完备
  • 使用简单
  • 功能完备

    FastJson的序列化操作

    JSON.toJSONString
    1、创建项目导入FastJson依赖,单元测试依赖,Lombok依赖
    2、创建实体类Student

    1. @Data
    2. public class Student {
    3. private int id;
    4. private String name;
    5. private int age;
    6. private String email;
    7. private Date birthday;
    8. }

    对象转JSON -对象-toJSONString

    结果为对象

  • 单元测试 ```java public class StudentTest { @Test public void testJosn(){

    1. Student student = new Student();
    2. student.setId(1);
    3. student.setName("张三");
    4. student.setAge(20);
    5. student.setEmail("zs@sina.com");
    6. student.setBirthday(new Date());
    7. String result = JSON.toJSONString(student);
    8. System.out.println(result);

    } }

  1. - 测试结果
  2. ```java
  3. {"age":20,"birthday":1656753454318,"email":"zs@sina.com","id":1,"name":"张三"}

List转JSON -数组-toJSONString

结果为数组,数组的元素为对象。

  • 单元测试

    1. @Test
    2. public void testJosnList(){
    3. Student student = new Student();
    4. student.setId(1);
    5. student.setName("张三");
    6. student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. Student student1 = new Student();
    10. student1.setId(2);
    11. student1.setName("李四");
    12. student1.setAge(24);
    13. student1.setEmail("ls@126.com");
    14. student1.setBirthday(new Date());
    15. ArrayList<Student> list = new ArrayList<>();
    16. list.add(student);
    17. list.add(student1);
    18. String result = JSON.toJSONString(list);
    19. System.out.println(result);
    20. }
  • 结果

    1. [{"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

  • 单元测试

    1. @Test
    2. public void testJosnMap(){
    3. Student student = new Student();
    4. student.setId(1);
    5. student.setName("张三");
    6. student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. Student student1 = new Student();
    10. student1.setId(2);
    11. student1.setName("李四");
    12. student1.setAge(24);
    13. student1.setEmail("ls@126.com");
    14. student1.setBirthday(new Date());
    15. Map<String,Student> map = new HashMap<>();
    16. map.put("student",student);
    17. map.put("student1",student1);
    18. String result = JSON.toJSONString(map);
    19. System.out.println(result);
    20. }
  • 结果

    1. {"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);

  • 单元测试

    1. @Test
    2. public void tsetJsonToJavaObject(){
    3. String jsonString="{\"age\":20,\"birthday\":1656753454318,"+
    4. "\"email\":\"zs@sina.com\",\"id\":1,\"name\":\"张三\"}";
    5. Student student = JSON.parseObject(jsonString, Student.class);
    6. System.out.println(student);
    7. }
  • 结果

    1. 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);

  • 单元测试

    1. @Test
    2. public void tsetJsonToJavaArray(){
    3. String jsonString="[{\"age\":20,\"birthday\":1656754123105,\"email\":\"zs@sina.com\"," +
    4. "\"id\":1,\"name\":\"张三\"},{\"age\":24,\"birthday\":1656754123105,\"email\":" +
    5. "\"ls@126.com\",\"id\":2,\"name\":\"李四\"}]";
    6. List<Student> list = JSON.parseArray(jsonString, Student.class);
    7. for(Student student:list){
    8. System.out.println(student);
    9. }
    10. }
  • 结果

    1. Student(id=1, name=张三, age=20, email=zs@sina.com, birthday=Sat Jul 02 17:28:43 CST 2022)
    2. 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>() {});

  • 单元测试

    1. @Test
    2. public void tsetJsonToJavaMap(){
    3. String jsonString="{\"student\":{\"age\":20,\"birthday\":1656754579419," +
    4. "\"email\":\"zs@sina.com\",\"id\":1,\"name\":\"张三\"},\"student1\"" +
    5. ":{\"age\":24,\"birthday\":1656754579419,\"email\":\"ls@126.com\"," +
    6. "\"id\":2,\"name\":\"李四\"}}";
    7. Map<String, Student> map = JSON.parseObject(jsonString,
    8. new TypeReference<Map<String, Student>>() {});
    9. for (String key:map.keySet()){
    10. System.out.println(key+"::"+map.get(key));
    11. }
    12. }
  • 结果

    1. student::Student(id=1, name=张三, age=20, email=zs@sina.com, birthday=Sat Jul 02 17:36:19 CST 2022)
    2. student1::Student(id=2, name=李四, age=24, email=ls@126.com, birthday=Sat Jul 02 17:36:19 CST 2022)

    SerializerFeature序列化对象

    WriteMapNullValue

    没有赋值的字符串属性,序列化时不会显示该属性,在序列化时加上该参数,序列化时该属性会显示为null

  • 单元测试

    1. @Test
    2. public void testNull(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
    10. // String result = JSON.toJSONString(student);
    11. System.out.println(result);
    12. }
  • 结果

    1. {"age":0,"birthday":1657007508874,"email":"zs@sina.com","id":1,"name":null}

    WriteNullStringAsEmpty

    将没有赋值的属性,显示为空字符串。

  • 单元测试

    1. @Test
    2. public void testNullToEmpty(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty);
    10. System.out.println(result);
    11. }
  • 结果

    1. {"age":0,"birthday":1657007677593,"email":"zs@sina.com","id":1,"name":""}

    WriteNullNumberAsZero

    将没有赋值的数值型的属性显示为0(注:版本默认将不赋值的数值型属性显示为0),该属性不会影响不赋值的字符串属性是否显示。

  • 单元测试

    1. @Test
    2. public void testNullToZero(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
    10. System.out.println(result);
    11. }
  • 结果

    1. @Test
    2. public void testNullToZero(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
    10. System.out.println(result);
    11. }

    WriteNullBooleanAsFalse

    将未赋值的Boolean值序列化为false。

  • 单元测试

    1. @Test
    2. public void testBooleanToFalse(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. // student.setFlag(true);
    10. String result = JSON.toJSONString(student, SerializerFeature.WriteNullBooleanAsFalse);
    11. System.out.println(result);
    12. }
  • 结果

    1. {"age":0,"birthday":1657008348277,"email":"zs@sina.com","flag":false,"id":1}

    WriteDateUseDateFormat

    将时间日期序列化为标准的格式。

  • 单元测试

    1. @Test
    2. public void testDateToFormat(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat);
    10. System.out.println(result);
    11. }
  • 结果

    1. {"age":0,"birthday":"2022-07-05 16:09:20","email":"zs@sina.com","id":1}

    PrettyFormat

    将输出的格式转化为纵向输出。

  • 单元测试

    1. @Test
    2. public void testDateToPrettyFormat(){
    3. Student student=new Student();
    4. student.setId(1);
    5. // student.setName("张三");
    6. // student.setAge(20);
    7. student.setEmail("zs@sina.com");
    8. student.setBirthday(new Date());
    9. String result = JSON.toJSONString(student, SerializerFeature.PrettyFormat);
    10. System.out.println(result);
    11. }
  • 结果

    1. {
    2. "age":0,
    3. "birthday":1657008638537,
    4. "email":"zs@sina.com",
    5. "id":1
    6. }

    JSonFields注解

    该注解可以使用在类上,方法上和参数上。

  • name属性,可以表明序列化后的字段名

  • ordinal属性,表明系列化后的字段的顺序,默认值为0,值越大越往后
  • format属性,表明序列化后的格式
  • serialize属性,是否序列化
  • disserialize属性,是否反序列化(注:未序列化的不可反序列化)
  • serialzeFeatures序列化时的特性定义。与SerializerFeature类似。

    1. @JSonFields(name="studentName",ordinal=1)
    2. @JSonFields(format="YYYY-MM-dd")
    3. @JSonFields(serialize="false")

    JSonType注解

    该注解作用在类上

  • includes属性,指明需要进行序列化的属性(注:与JSonFields注解冲突时,JSonType优先)

  • orders属性,指明序列化后的顺序。
  • serialzeFeatures属性,与SerializerFeature类似
    1. @JSonType(includes={"id","name","address"},orders={"name","address","age"})