首先引入依赖包,这里使用的是alibaba的fastjson;

<!– fastjson依赖 –>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
Java对象转成JSON格式
import com.alibaba.fastjson.JSON;
import com.example.study.entity.Student;

public class JsonStudy {
public static void main(String[] args) {
Student student = new Student(“123″,”张三”,18);
//直接输出,结果为:Student(id=123, name=张三, age=18)
System.out.println(student);
//转换为JSON格式输出,以下两种方法只有返回值不同
Object objectJson = JSON.toJSON(student);
String stringJSON = JSON.toJSONString(student);
//结果为:{“name”:”张三”,”id”:”123″,”age”:18}
System.out.println(objectJson);
//结果为:{“name”:”张三”,”id”:”123″,”age”:18}
System.out.println(stringJSON);
}
}
JSON格式字符串转换成Java对象
直接将JSON字符串转换成Java对象

import com.alibaba.fastjson.JSONObject;
import com.example.study.entity.Student;

public class JsonStudy {
public static void main(String[] args) {
//定义一个student类型的JSON字符串
String json = “{\”name\”:\”张三\”,\”id\”:\”123\”,\”age\”:18}”;
//将这个JSON字符串转换成Student对象
Student student = JSONObject.parseObject(json, Student.class);
//输出结果为:Student(id=123, name=张三, age=18)
System.out.println(student);
//输出结果为:张三
System.out.println(student.getName());
}
}
先将JSON字符串转换成JSON对象,再转换成Java对象

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.study.entity.Student;

public class JsonStudy {
public static void main(String[] args) {
//定义一个student类型的JSON字符串
String json = “{\”name\”:\”张三\”,\”id\”:\”123\”,\”age\”:18}”;
//将这个JSON字符串转换成JSON对象
JSONObject jsonObject = JSONObject.parseObject(json);
//将JSON对象转换成Java对象
Student student1 = JSONObject.toJavaObject(jsonObject, Student.class);
Student student2 = JSON.toJavaObject(jsonObject, Student.class);

//输出结果为:Student(id=123, name=张三, age=18)
System.out.println(student1);
//输出结果为:张三
System.out.println(student1.getName());

//输出结果为:Student(id=123, name=张三, age=18)
System.out.println(student2);
//输出结果为:张三
System.out.println(student2.getName());
}
}
如果JSON字符串是一个JSON数组,并且数组里面存放的同一种类型的对象,可以将这个JSON数组转换成Java的List对象;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.study.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class JsonStudy {
public static void main(String[] args) {
//new一个list对象
List<Student> studentList = new ArrayList<>();
studentList.add(new Student(“111″,”张三”,18));
studentList.add(new Student(“222″,”李四”,20));
studentList.add(new Student(“333″,”王五”,23));

//获取list的JSON数组形式字符串
String listJsonString = JSON.toJSONString(studentList);
//结果为:[{“age”:18,”id”:”111″,”name”:”张三”},{“age”:20,”id”:”222″,”name”:”李四”},{“age”:23,”id”:”333″,”name”:”王五”}]
System.out.println(listJsonString);

//将JSON字符串转换成List对象,List里面放的Student对象
List<Student> students = JSONObject.parseArray(listJsonString, Student.class);
//结果为:[Student(id=111, name=张三, age=18), Student(id=222, name=李四, age=20), Student(id=333, name=王五, age=23)]
System.out.println(students);
//结果为:3
System.out.println(students.size());
//结果为:Student(id=333, name=王五, age=23)
System.out.println(students.get(2));
}
}

版权声明:本文为liftsail原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liftsail/p/16484318.html