高性能JSON 框架之fastJSON

目录

1.FastJson的介绍:

JSON协议使用方便,越来越流行,JSON的处理器有很多,这里我介绍一下FastJson,FastJson是阿里的开源框架,被不少企业使用,是一个极其优秀的Json框架,Github地址

2.FastJson的特点:

1.FastJson数度快,无论序列化和反序列化,都是当之无愧的fast 
2.功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum) 
3.零依赖(没有依赖其它任何类库)

3.FastJson的简单说明:

FastJson对于json格式字符串的解析主要用到了下面三个类: 
1.JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换 
2.JSONObject:fastJson提供的json对象 
3.JSONArray:fastJson提供json数组对象

4.FastJson的用法

首先定义三个json格式的字符串

        private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";

	private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";

	private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";

	/**
	 * json字符串-简单对象型到JSONObject的转换
	 */
	@Test
	public void testJSONStrToJSONObject() {
		JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
		System.out.println("studentName: " + jsonObject.getString("studentName") + ":" + " studentAge: " + jsonObject.getInteger("studentAge"));
	}

	/**
	 * JSONObject到json字符串-简单对象型的转换
	 */
	@Test
	public void testJSONObjectToJSONStr() {
		//将string 转换为  JSONObject
		JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
		//第一种方式
		String jsonString = JSONObject.toJSONString(jsonObject);
		System.out.println("第一种方式:" + jsonString);
		//第二种方式
		String jsonString2 = jsonObject.toJSONString();
		System.out.println("第二种方式:" + jsonString2);
	}

	/**
	 * json字符串-数组类型到JSONArray的转换
	 */
	@Test
	public void testJSONStrToJSONArray() {
		JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
		//遍历方式
		int size = jsonArray.size();
		System.out.println("==========第一种遍历方式============");
		for (int i = 0; i < size; i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			System.out.println("studentName : " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
				+ jsonObject.getInteger("studentAge"));
		}
		//遍历方式2
		System.out.println("==========第二种遍历方式============");
		for (Object obj : jsonArray) {
			JSONObject jsonObject = (JSONObject) obj;
			System.out.println("studentName:  " + jsonObject.getString("studentName") + ":" + "  studentAge:  "
				+ jsonObject.getInteger("studentAge"));
		}

	}

	/**
	 * JSONArray到json字符串-数组类型的转换
	 */
	@Test
	public void testJSONArrayToJSONStr() {
		//已知JSONArray,目标要转换为json字符串
		JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
		//第一种方式
		System.out.println("==========第一种遍历方式============");
		String jsonString = JSONArray.toJSONString(jsonArray);
		System.out.println(jsonString);
		//第二种方式
		System.out.println("==========第二种遍历方式============");
		String jsonString2 = jsonArray.toJSONString(jsonArray);
		System.out.println(jsonString2);
	}


	/**
	 * 复杂json格式字符串到JSONObject的转换
	 */
	@Test
	public void testComplexJSONStrToJSONObject() {
		JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
		String teacherName = jsonObject.getString("teacherName");
		Integer teacherAge = jsonObject.getInteger("teacherAge");
		System.out.println("teacherName :" + teacherName + " teacherAge :" + teacherAge);
		JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");
		//获取JSONObject 中的数据
		String courseName = jsonObjectcourse.getString("courseName");
		Integer code = jsonObjectcourse.getInteger("code");
		System.out.println("courseName: " + courseName + " code: " + code);
		JSONArray jsonArrayStudents = jsonObject.getJSONArray("students");
		//遍历JSONArray
		for (Object object : jsonArrayStudents) {
			JSONObject jsonObjectone = (JSONObject) object;
			String studentName = jsonObjectone.getString("studentName");
			Integer studentAge = jsonObjectone.getInteger("studentAge");
			System.out.println("studentName:  " + studentName + " studentAge:" + studentAge);
		}
	}

	/**
	 * 复杂JSONObject到json格式字符串的转换
	 */
	@Test
	public void testJSONObjectToComplexJSONStr() {
		//复杂JSONObject ,目标要转换为json 字符串
		JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
		//第一种方式
		String jsonString = JSONObject.toJSONString(jsonObject);
		System.out.println(jsonString);
		//第二种方式
		String jsonString2 = jsonObject.toJSONString();
		System.out.println(jsonString2);
	}

	/**
	 * json字符串-简单对象到JavaBean之间的转换
	 */
	@Test
	public void testJSONStrToJavaBeanObj() {
		//第一种方式
		JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
		String studentName = jsonObject.getString("studentName");
		Integer studentAge = jsonObject.getInteger("studentAge");
		Student student1 = new Student(studentName, studentAge);
		System.out.println(student1);
		//第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		Student student2 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {
		});
		System.out.println(student2);
		//第三种方式,使用Gson的思想
		Student student3 = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
		System.out.println(student3);
	}

	/**
	 * JavaBean到json字符串-简单对象的转换
	 */
	@Test
	public void testJavaBeanObjToJSONStr() {
		Student student = new Student("jack", 21);
		String jsonString = JSONObject.toJSONString(student);
		System.out.println(jsonString);
	}

	/**
	 * json字符串-数组类型到JavaBean_List的转换
	 */
	@Test
	public void testJSONStrToJavaBeanList() {
		//第一种方式
		JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
		//遍历JSONArray
		List<Student> students = new ArrayList<>();
		Student student = null;
		for (Object object : jsonArray) {
			JSONObject jsonObjectone = (JSONObject) object;
			String studentName = jsonObjectone.getString("studentName");
			Integer studentAge = jsonObjectone.getInteger("studentAge");

			student = new Student(studentName, studentAge);
			students.add(student);
		}
		System.out.println("students:  " + students);
		//第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		List<Student> studentList = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {
		});
		System.out.println("studentList:  " + studentList);

		//第三种方式,使用Gson的思想
		List<Student> studentList1 = JSONArray.parseArray(JSON_ARRAY_STR, Student.class);
		System.out.println("studentList1:  " + studentList1);
	}

	/**
	 * JavaBean_List到json字符串-数组类型的转换
	 */
	@Test
	public void testJavaBeanListToJSONStr() {
		Student student = new Student("jack", 12);
		Student studentTwo = new Student("rows", 13);
		List<Student> students = new ArrayList<>();
		students.add(student);
		students.add(studentTwo);
		String jsonString = JSONArray.toJSONString(students);
		System.out.println(jsonString);
	}

	/**
	 * 复杂json格式字符串到JavaBean_obj的转换
	 */
	@Test
	public void testComplexJSONStrToJavaBean() {
		//第一种方式 使用  TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {
		});
		System.out.println(teacher);

		//第二种方式,使用Gson思想
		Teacher teacher1 = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);
		System.out.println(teacher1);
	}

	/**
	 * 复杂JavaBean_obj到json格式字符串的转换
	 */
	@Test
	public void testJavaBeanToComplexJSONStr() {
		//已知复杂JavaBean_obj
		Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {
		});
		String jsonString = JSONObject.toJSONString(teacher);
		System.out.println(jsonString);
	}

	/**
	 * 简单JavaBean_obj到json对象的转换
	 */
	@Test
	public void testJavaBeanToJSONObject() {
		//已知简单JavaBean_obj
		Student student = new Student("lily", 12);

		//方式一
		String jsonString = JSONObject.toJSONString(student);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		System.out.println("方式一:" + jsonObject);

		//方式二
		JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student);
		System.out.println("方式二:" + jsonObject1);
	}

	/**
	 * 简单json对象到JavaBean_obj的转换
	 */
	@Test
	public void testJSONObjectToJavaBean() {
		//已知简单json对象
		JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

		//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {
		});
		System.out.println(student);

		//第二种方式,使用Gson的思想
		Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
		System.out.println(student1);
	}

	/**
	 * JavaList到JsonArray的转换
	 */
	@Test
	public void testJavaListToJsonArray() {
		//已知JavaList
		Student student = new Student("lily", 12);
		Student studenttwo = new Student("lucy", 15);
		List<Student> students = new ArrayList<Student>();
		students.add(student);
		students.add(studenttwo);
		//方式一
		String jsonString = JSONArray.toJSONString(students);
		JSONArray jsonArray = JSONArray.parseArray(jsonString);
		System.out.println("方式一:" + jsonArray);
		//方式二
		JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students);
		System.out.println("方式二" + jsonArray1);
	}

	/**
	 * JsonArray到JavaList的转换
	 */
	@Test
	public void testJsonArrayToJavaList() {
		//已知JsonArray
		JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
		//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(),
			new TypeReference<ArrayList<Student>>() {
			});
		System.out.println(students);
		//第二种方式,使用Gson的思想
		List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);
		System.out.println(students1);
	}

	/**
	 * 复杂JavaBean_obj到json对象的转换
	 */
	@Test
	public void testComplexJavaBeanToJSONObject() {
		//已知复杂JavaBean_obj
		Student student = new Student("lily", 12);
		Student studenttwo = new Student("lucy", 15);

		List<Student> students = new ArrayList<Student>();
		students.add(student);
		students.add(studenttwo);
		Course course = new Course("english", 1270);

		Teacher teacher = new Teacher("crystall", 27, course, students);

		//方式一
		String jsonString = JSONObject.toJSONString(teacher);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		System.out.println(jsonObject);

		//方式二
		JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(teacher);
		System.out.println(jsonObject1);
	}

	/**
	 * 复杂json对象到JavaBean_obj的转换
	 */
	@Test
	public void testComplexJSONObjectToJavaBean() {
		//已知复杂json对象
		JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);

		//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
		Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Teacher>() {
		});
		System.out.println(teacher);

		//第二种方式,使用Gson的思想
		Teacher teacher1 = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class);
		System.out.println(teacher1);
	}

实体类:Teacher

import java.io.Serializable;
import java.util.List;

public class Teacher implements Serializable {

	private String teacherName;
	private Integer teacherAge;
	private Course course;
	private List<Student> students;

	public Teacher() {
	}

	public Teacher(String teacherName, Integer teacherAge, Course course, List<Student> students) {
		this.teacherName = teacherName;
		this.teacherAge = teacherAge;
		this.course = course;
		this.students = students;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public Integer getTeacherAge() {
		return teacherAge;
	}

	public void setTeacherAge(Integer teacherAge) {
		this.teacherAge = teacherAge;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}


	@Override
	public String toString() {
		return "Teacher{" +
			"teacherName='" + teacherName + '\'' +
			", teacherAge=" + teacherAge +
			", course=" + course +
			", students=" + students +
			'}';
	}
}

实体类:Student

import java.io.Serializable;

public class Student implements Serializable {
	private String studentName;
	private Integer studentAge;

	public Student() {
	}

	public Student(String studentName, Integer studentAge) {
		this.studentName = studentName;
		this.studentAge = studentAge;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public Integer getStudentAge() {
		return studentAge;
	}

	public void setStudentAge(Integer studentAge) {
		this.studentAge = studentAge;
	}

	@Override
	public String toString() {
		return "Student{" +
			"studentName='" + studentName + '\'' +
			", studentAge=" + studentAge +
			'}';
	}
}

实体类:Course

import java.io.Serializable;

public class Course implements Serializable {
	private String courseName;
	private Integer code;

	public Course() {
	}

	public Course(String courseName, Integer code) {
		this.courseName = courseName;
		this.code = code;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	@Override
	public String toString() {
		return "Course{" +
			"courseName='" + courseName + '\'' +
			", code=" + code +
			'}';
	}
}

实体类:Tutor

import java.io.Serializable;

public class Tutor implements Serializable {
	private Integer tutorId;
	private String name;
	private String email;

	public Tutor() {
	}

	public Tutor(String name, String email) {
		this.name = name;
		this.email = email;
	}

	public Tutor(Integer tutorId, String name, String email) {
		this.tutorId = tutorId;
		this.name = name;
		this.email = email;
	}


	public Integer getTutorId() {
		return tutorId;
	}

	public void setTutorId(Integer tutorId) {
		this.tutorId = tutorId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "Tutor{" +
			"tutorId=" + tutorId +
			", name='" + name + '\'' +
			", email='" + email + '\'' +
			'}';
	}
}

结束。。。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Fastjson是阿里巴巴的一个开源的Java库,可以用于解析和生成JSON数据。解析复杂的JSON数据需要创建一个Java类来映射JSON数据的结构,然后使用Fastjson API将JSON字符串解析为Java对象。如果你想要解析一个复杂的JSON数据,你可以尝试如下方法: 1. 定义一个Java类来映射JSON数据的结构。每一个字段对应一个键值对。 2. 使用Fastjson的API,如parseObject,将JSON字符串解析为一个Java对象。 3. 使用解析后的Java对象访问数据。 代码示例: ``` String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"; //1. 定义一个Java类来映射JSON数据的结构 class User { private String name; private int age; private String city; //getters and setters } //2. 使用Fastjson的API,将JSON字符串解析为一个Java对象 User user = JSON.parseObject(json, User.class); //3. 使用解析后的Java对象访问数据 System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getCity()); ``` ### 回答2: Fastjson是一个Java的解析JSON库,它快速、稳定、易用,可以处理各种复杂的JSON数据结构。Fastjson的解析速度非常快,可以提高程序的性能,大大减少了JSON数据解析与Java对象转换的时间,利用了Java语言的强大性能优势,使得Fastjson成为了一款非常受欢迎的JSON解析库之一。 Fastjson可以快速处理各种JSON数据格式,包括对象、数组、字符串、数字等,可以轻松地将JSON数据转换为Java对象或Map,也可以将Java对象转换为JSON字符串。Fastjson采用的是基于Bean的转换,即将JSON数据转换为Java对象时,会根据JSON数据自动创建Java对象,并将JSON数据中的字段映射到Java对象中对应的属性中。 Fastjson可以解析复杂的JSON数据,例如包含嵌套对象和数组的JSON数据。Fastjson提供了很多高级的特性,例如自动类型识别、处理循环引用、支持序列化与反序列化、支持自定义序列化规则等,这些特性使得Fastjson在处理复杂JSON数据结构时更为灵活和可靠。 Fastjson还可以通过注解的方式来设置Java对象与JSON数据之间的映射规则,提高了程序的可维护性和可读性。通过注解可以自动设置Java属性与JSON字段之间的映射,也可以忽略不需要的属性,还可以自定义转换器来实现自定义的序列化与反序列化规则。 在实际开发中,Fastjson是一款非常实用的JSON解析库,它可以轻松地处理各种复杂的JSON数据结构,提高程序的性能和可维护性,方便开发人员快速地开发出高质量的应用程序。 ### 回答3: Fastjson是一种高效且易于使用的Java JSON解析框架。它可以帮助Java开发人员快速地将JSON数据转换为Java对象,同时保持数据的完整性和准确性。 Fastjson支持解析不同复杂度的JSON数据,如简单JSON对象,嵌套的JSON对象,数组和嵌套的数组。为了解析复杂JSON数据,Fastjson提供了几种重要的功能和API: 1. JSON对象和JSON数组 对于一个复杂的JSON数据,它可能包含许多嵌套的JSON对象和JSON数组。Fastjson提供了JSONObject和JSONArray类,可以帮助我们处理这些结构数据。我们可以使用getJSONObject()和getJSONArray()方法从JSONObject或JSONArray中获取子对象和子数组。 2.反序列化 反序列化是将JSON数据转换为Java对象的过程。Fastjson提供了一个JSON.parseObject()方法,可以将JSON字符串转换为Java对象。例如,我们可以使用下面的代码将一个复杂JSON字符串解析为一个Java对象: String jsonString = "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\"}}"; Person person = JSON.parseObject(jsonString, Person.class); 3.注解 Fastjson提供了一些注解来帮助Java开发人员更灵活地控制对象的转换和序列化过程。例如,@JSONField注解可以指定JSON字段的名称和顺序,@JSONCreator注解可以指定一个特殊的构造函数,用于反序列化操作。 4.特殊对象转换器 有些复杂的JSON数据可能包含自定义的对象类型,无法直接通过Fastjson的默认转换器进行序列化和反序列化。Fastjson提供了ObjectSerializer和ObjectDeserializer接口,可以实现自定义的对象转换器。 总之,Fastjson是一种非常强大、稳定和易于使用的JSON框架,可以帮助Java开发人员快速高效地处理复杂和大数据量的JSON数据。它的灵活性和可定制性也让它成为许多企业级应用程序中首选的JSON解析库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值