JSON入门学习

package com.my.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.my.entity.Student;
import com.my.entity.Teacher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

/**
 * @author : lijie
 * @version V1.0
 * @Description:
 * @date Date : 2021年07月15日 21:05
 */

public class JSONDemo {

  /**
   * 单个实例:bean<==>json
   */
  @Test
  public void test1() {
    System.out.println("------The Result of test1------");
    // bean==>json
    Student s1 = new Student(1, "zhangsan", 18);
    String s = JSON.toJSONString(s1);
    System.out.println(s);
    // json ==> bean
    String json = "{\"age\":18,\"id\":1,\"name\":\"zhangsan\"}";
    // 1.直接利用JSONObject来获取相应属性值
    System.out.println("--------------1--------------");
    JSONObject jsonObject = JSON.parseObject(json);
    String name = jsonObject.getString("name");
    System.out.println(name);
    int age = jsonObject.getIntValue("age");
    System.out.println(age);
    // 2.将串转成相应java对象,然后再获取属性
    System.out.println("--------------2--------------");
    Student student = JSON.parseObject(json, Student.class);
    System.out.println(student);

  }

  /**
   * 多个实例:bean<==>json
   */
  @Test
  public void test2() {
    System.out.println("------The Result of test2------");
    // bean==>json
    List<Student> students = Arrays
        .asList(new Student(1, "zhangsan", 18), new Student(2, "lisi", 19));
    String studentsJSON = JSON.toJSONString(students);
    System.out.println(studentsJSON);
    // json ==> bean
    String json = "[{\"age\":18,\"id\":1,\"name\":\"zhangsan\"},{\"age\":19,\"id\":2,\"name\":\"lisi\"}]";
    // 利用JSONArray获取相应属性值
    JSONArray jsonArray = JSON.parseArray(json);
    System.out.println(jsonArray.get(0));
    System.out.println(jsonArray.get(1));
    // 直接获取相应的bean对象集合
    List<Student> studentList = JSON.parseArray(json, Student.class);
    System.out.println(studentList);
  }

  @Test
  public void test3() {
    System.out.println("------The Result of test3------");

    List<Student> students = Arrays
        .asList(new Student(1, "zhangsan", 18), new Student(2, "lisi", 19));

    List<Student> students1 = Arrays
        .asList(new Student(3, "wangwu", 20), new Student(4, "qianliu", 21));

    List<List<Student>> lists = new ArrayList<>();
    lists.add(students);
    lists.add(students1);
    String jsonString = JSON.toJSONString(lists);
    System.out.println(jsonString);
    System.out.println("------------------------------------");
    String json = "[[{\"age\":18,\"id\":1,\"name\":\"zhangsan\"},{\"age\":19,\"id\":2,\"name\":\"lisi\"}],[{\"age\":20,\"id\":3,\"name\":\"wangwu\"},{\"age\":21,\"id\":4,\"name\":\"qianliu\"}]]";
    JSONArray jsonArray = JSON.parseArray(json);
    System.out.println(jsonArray.get(0));
    System.out.println(jsonArray.get(1));
    String s = JSON.toJSONString(jsonArray.get(1));
    JSONArray jsonArray1 = JSON.parseArray(s);
    System.out.println(jsonArray1.get(0));
    System.out.println(jsonArray1.get(1));

    List<Student> students2 = JSON.parseArray(s, Student.class);
    System.out.println(students2);

  }
}

===========================================结果如下==================================================
------The Result of test1------ {"age":18,"id":1,"name":"zhangsan"} --------------1-------------- zhangsan 18 --------------2-------------- Student(id=1, name=zhangsan, age=18) ------The Result of test2------ [{"age":18,"id":1,"name":"zhangsan"},{"age":19,"id":2,"name":"lisi"}] {"name":"zhangsan","id":1,"age":18} {"name":"lisi","id":2,"age":19} [Student(id=1, name=zhangsan, age=18), Student(id=2, name=lisi, age=19)] ------The Result of test3------ [[{"age":18,"id":1,"name":"zhangsan"},{"age":19,"id":2,"name":"lisi"}],[{"age":20,"id":3,"name":"wangwu"},{"age":21,"id":4,"name":"qianliu"}]] ------------------------------------ [{"name":"zhangsan","id":1,"age":18},{"name":"lisi","id":2,"age":19}] [{"name":"wangwu","id":3,"age":20},{"name":"qianliu","id":4,"age":21}] {"name":"wangwu","id":3,"age":20} {"name":"qianliu","id":4,"age":21} [Student(id=3, name=wangwu, age=20), Student(id=4, name=qianliu, age=21)]

 

 

 

 

 

 

 

 

 

 

 

https://blog.csdn.net/weixin_46008168/article/details/106314239?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162618852816780269896321%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162618852816780269896321&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~times_rank-1-106314239.first_rank_v2_pc_rank_v29_1&utm_term=json%E5%AD%A6%E4%B9%A0&spm=1018.2226.3001.4187

 

 

https://blog.csdn.net/qq_41986648/article/details/104779298?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162618852816780269896321%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162618852816780269896321&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~times_rank-3-104779298.first_rank_v2_pc_rank_v29_1&utm_term=json%E5%AD%A6%E4%B9%A0&spm=1018.2226.3001.4187

posted @ 2021-07-13 23:27  杰然不同2019  阅读(74)  评论(0编辑  收藏  举报