com.alibaba.fastjson Note [vaynexiao]

什么是JSON对象

{
    
    
	"ID": 1001,
	"name": "张三",
	"age": 24
}

1:数据在花括号中
2:数据以"键:值"对的形式出现(其中键多以字符串形式出现,值可取字符串,数值,甚至其他json对象)
3:每两个"键:值"对以逗号分隔

什么是JSON对象数组

[
	{
    
    "ID": 1001, "name": "张三", "age": 24},
	{
    
    "ID": 1002, "name": "李四", "age": 25},
	{
    
    "ID": 1003, "name": "王五", "age": 22}
]

1:数据在方括号中,逗号隔开
2:方括号中每个数据以json对象形式出现
3:每两个数据以逗号分隔

com.alibaba.fastjson

fastjson.jar是阿里开发的一款专门用于Java开发的包,可以方便的
    实现json对象与JavaBean对象的转换,
    实现JavaBean对象与json字符串的转换,
    实现json对象与json字符串的转换。
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.61</version>
</dependency>

关系分析:
主要的3个类,JSON,JSONArray,JSONObject
class JSONObject extends JSON implements Map
class JSONArray extends JSON implements List

JSONObject

原生生成JSONObject
JSONObject zhangsan = new JSONObject();
zhangsan.put("name", "张三");
System.out.println(zhangsan.toString());

通过hashMap数据结构生成JSONObject
HashMap<String, Object> zhangsan = new HashMap<>();
zhangsan.put("name", "张三");
System.out.println(new JSONObject(zhangsan).toString());

通过实体生成JSONObject
Student student = new Student();
student.setId(1);
student.setAge("20");
student.setName("张三");

System.out.println(JSON.toJSON(student)); //生成json格式

String stuString = JSONObject.toJSONString(student); //对象转成string

JSON字符串转换成JSON对象
String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";
JSONObject jsonObject1 = JSONObject.parseObject(stuString);
System.out.println(jsonObject1);

相互转换

        List<Student> list = new ArrayList<Student>();
        Student student = new Student("bob",24);
        Student student2 = new Student("lily", 23);
        list.add(student);
        list.add(student2);
        System.out.println ("=======javaBean  to  String=======");
        String str1 = JSON.toJSONString(student);
        System.out.println(str1); // {"age":24,"name":"bob"}
        System.out.println(JSON.toJSONString(list));
        System.out.println(); // [{"age":24,"name":"bob"},{"age":23,"name":"lily"}]



        System.out.println("======javaBean to jsonObject======");
        JSONObject jsonObject1 = (JSONObject)JSON.toJSON(student);
        System.out.println(jsonObject1.getString("name"));  // bob
        System.out.println();



        System.out.println("=======javaBean to jsonArray======");
        List<Student> stulist = new ArrayList<Student>();
        for(int i=0;i<5;i++){
    
    
            stulist.add(new Student("student"+i, i));
        }
        JSONArray jsonArrays = (JSONArray) JSON.toJSON(stulist);
        for(int i=0;i<jsonArrays.size();i++){
    
    
            System.out.println(jsonArrays.getJSONObject(i));
        }
        System.out.println();
//		{"name":"student0","age":0}
//		{"name":"student1","age":1}
//		{"name":"student2","age":2}
//		{"name":"student3","age":3}
//		{"name":"student4","age":4}

        System.out.println("======jsonObject to javaBean======");
        Student studentObj = JSON.toJavaObject(jsonObject1, Student.class);
        System.out.println(studentObj.toString()); // Student{name='bob', age=24}
        System.out.println();

        System.out.println("=====jsonObject to String=====");
        String str4 = JSON.toJSONString(jsonObject1);
        System.out.println(str4); // {"name":"bob","age":24}
        System.out.println();


        System.out.println("===== jsonArry to List ======");
        List<Student> myList = new ArrayList<Student>();
        for(int i=0;i<jsonArrays.size();i++){
    
    
            Student student3 = JSON.toJavaObject(jsonArrays.getJSONObject(i), Student.class);
            myList.add(student3);
        }
        for(Student stu:myList){
    
    
            System.out.println(stu);
        }
        System.out.println();
//        Student{name='student0', age=0}
//        Student{name='student1', age=1}
//        Student{name='student2', age=2}
//        Student{name='student3', age=3}
//        Student{name='student4', age=4}


        System.out.println("=======jsonString to jsonObject=====");
		//这里特别申明,不是string那么简单,string要符合json的数据格式,才是jsonString
		//都是String,但jsonString有额外使用场所那就是转为json
        JSONObject jso1=JSON.parseObject(str1);
        System.out.println(jso1.getString("name")); // bob
        System.out.println();

        System.out.println("=====jsonString to jsonArray=====");
        JSONArray jArray = JSON.parseArray(JSON.toJSONString(stulist));
        for(int i=0;i<jArray.size();i++){
    
    
            System.out.println(jArray.getJSONObject(i));
        }
        System.out.println();
//		{"name":"student0","age":0}
//		{"name":"student1","age":1}
//		{"name":"student2","age":2}
//		{"name":"student3","age":3}
//		{"name":"student4","age":4}


        System.out.println("======jsonString to javaBean=======");
        String str9 = "{\"name\":\"student4\",\"age\":4}";
        Student stu1 = JSON.parseObject(str9, Student.class);
        System.out.println(stu1.toString()); // Student{name='bob', age=24}
        System.out.println();

	    System.out.println("======List - jsonString - JSON - jsonArray=======");
	   //List中包含多个Student
        ArrayList<Student> studentLsit = new ArrayList<>();
        Student stu1 = new Student();
        stu1.setAge(20);
        stu1.setName("张三");
        studentLsit.add(stu1);
        Student student2 = new Student();
        student2.setAge(22);
        student2.setName("李四");
        studentLsit.add(student2);
        //list转json,再转成符合json格式的String,也就是jsonString
        String str23 = JSON.toJSON( studentLsit ).toString();
        System.out.println("1:"+ (JSONArray)JSON.toJSON( studentLsit ) );
        System.out.println("2:"+ JSONObject.parseArray( str23 ));
        System.out.println("3:"+ str23);
        //jsonString 转 JSONObject
        //JSONObject j = JSONObject.parseObject( str23 ); 
        //这里student1.toString()不符合json格式,stu1是多个元素,当然不能转成JSONObject
        JSONArray jsonArray = JSONObject.parseArray( str23 );
        System.out.println("5:"+ jsonArray);

猜你喜欢

转载自blog.csdn.net/vayne_xiao/article/details/109691914