Json详解以及fastjson使用教程

407 阅读1分钟

Json是一种轻量级的数据交换格式,采用一种“键:值”对的文本格式来存储和表示数据,在系统交换数据过程中常常被使用,是一种理想的数据交换语言。在使用Java做Web开发时,不可避免的会遇到Json的使用。下面我们就简单讲一下Json的使用以及fastjson.jar包的使用。

package jsonTest;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference;

public class MyJson {

public static void main(String[] args) {
	
	List<Student> list=new ArrayList<>();
	Student student=new Student("bob",24);
	Student student12=new Student("lily", 23);
	list.add(student);
	list.add(student12);
	System.out.println("*******javaBean  to jsonString*******");
	String str1=JSON.toJSONString(student);
	System.out.println(str1);
	System.out.println(JSON.toJSONString(list));
	System.out.println();
	
	System.out.println("******jsonString to javaBean*******");
	//Student stu1=JSON.parseObject(str1,new TypeReference<Student>(){});
	Student stu1=JSON.parseObject(str1,Student.class);
	System.out.println(stu1);
	System.out.println();
	
	System.out.println("******javaBean to jsonObject******");
	JSONObject jsonObject1=(JSONObject)JSON.toJSON(student);
	System.out.println(jsonObject1.getString("name"));
	System.out.println();
	
	System.out.println("******jsonObject to javaBean******");
	Student student2=JSON.toJavaObject(jsonObject1, Student.class);
	System.out.println(student2);
	System.out.println();
	
	System.out.println("*******javaBean to jsonArray******");
	List<Student> stulist=new ArrayList<>();
	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();
	
	System.out.println("*****jsonArry to javalist******");
	List<Student> myList=new ArrayList<>();
	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();
	
	System.out.println("*****jsonObject to jsonString*****");
	String str4=JSON.toJSONString(jsonObject1);
	System.out.println(str4);
	System.out.println();
	
	System.out.println("*******jsonString to jsonObject*****");
	JSONObject jso1=JSON.parseObject(str1);
	System.out.println(jso1.getString("name"));
	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();
}

}

———————————————— 版权声明:本文为CSDN博主「名字真难娶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/srj10955305…