文章目录
- 前言
- 一、什么是JSON对象?
- 二、什么是json字符串?
- 二、什么是JSON数组?
- 三、复杂、嵌套的json字符串
- 四、json字符串转换
- 4.1 简单json字符串转换为java对象
- 4.2 简单json字符串数组转换为list数组
- 4.3 复杂嵌套json字符串数组转换为Java对象、list数组
- 4.3.1 示例1
- 4.3.2 示例2
- 总结
前言
在开发中经常需要将json字符串转换为java对象或者list数组,这样方便我们来使用这些数据,下面就来介绍一下怎样将json字符串和json数组转换为Java对象或者list数组。
本次使用阿里巴巴的fastjson来解析json字符串,需要添加依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </dependency>
一、什么是JSON对象?
在大括号 {…} 中书写,包含多个 key/value(键/值)对,key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)
例如一个最简单的没有嵌套的JSON对象:
{ “name”:“Sun”, “age”:18, “site”:null }
二、什么是json字符串?
书写格式和JSON对象一样,不过类型属于String,不能直接操作其中的各个属性,需要对其进行转换。在很多前后端交互传值之类的,都以json字符串的形式进行传递。,所以对json字符串进行转换非常有必要。
二、什么是JSON数组?
JSON 数组在中括号中书写,JSON 中数组值必须是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。
简单的JSON数组:
[ “Google”, “Runoob”, “Taobao” ]
三、复杂、嵌套的json字符串
复杂的是json字符串里面嵌套json数组,这也是转换的重点,例如:
{
“student”: [
{
“name”: “Tom”,
“Grade”: 1,
“age”: 11,
“gender”: “M”
},
{
“name”: “Jerry”,
“Grade”: 1,
“age”: 10,
“gender”: “M”
}
],
“classroom”: {
“class1”: “room1”,
“class2”: “room2”
}
}
在这里推荐几个比较好用的json字符串校验和格式化的网站:
链接: http://www.kjson.com/
链接: https://www.bejson.com/
四、json字符串转换
4.1 简单json字符串转换为java对象
转换语句:
//方法1 将json字符串转换为java对象Student student =JSON.parseObject(jsonStr,Student .class);
//方法2 先将json字符串转换成JSONObject对象JSONObject jsonObject1 = new JSONObject(JSON.parseObject(jsonStr));//将转换后的JSONObject对象转换成Student对象Student student1 = (Student) JSONObject.toJavaObject(jsonObject1,Student.class);
示例:解析如下json字符串
{
“name”: “Tom”,
“Grade”: 1,
“age”: 11,
“gender”: “M”
}
首先创建一个对应的对象:
public class Student { private String name; private int grade; private int age; private String gender; //省略get、set方法}
public void testJson(){ String jsonStr = "{\n" + " \"name\": \"Tom\", \n" + " \"Grade\": 1, \n" + " \"age\": 11, \n" + " \"gender\": \"M\"\n" + " }"; JSONObject jsonObject = new JSONObject(JSON.parseObject(jsonStr)); System.out.println("jsonObject:"+jsonObject); System.out.println("name:"+jsonObject.get("name")); System.out.println("Grade:"+jsonObject.get("Grade")); System.out.println("age:"+jsonObject.get("age")); //方法1 将json字符串转换为java对象 Student student =JSON.parseObject(jsonStr,Student .class); System.out.println("student:"+student); //方法2 先将json字符串转换成JSONObject对象 JSONObject jsonObject1 = new JSONObject(JSON.parseObject(jsonStr)); //将转换后的JSONObject对象转换成Student对象 Student student1 = (Student) JSONObject.toJavaObject(jsonObject1,Student.class); System.out.println("student1:"+student1); //转换为java对象之后可以利用对象的get方法来取值 System.out.println("age:"+student.getAge()); }
执行结果:
4.2 简单json字符串数组转换为list数组
使用语句:
List<Student> studentList = JSONObject.parseArray(jsonStr, Student.class);
示例:
[
{
“name”: “Tom”,
“Grade”: 1,
“age”: 11,
“gender”: “M”
},
{
“name”: “Jerry”,
“Grade”: 1,
“age”: 10,
“gender”: “M”
}
]
首先创建一个对应的java对象:
public class Student { private String name; private int grade; private int age; private String gender; //省略get、set方法}
public void testJson(){ String jsonStr = "[\n" + " {\n" + " \"name\": \"Tom\", \n" + " \"Grade\": 1, \n" + " \"age\": 11, \n" + " \"gender\": \"M\"\n" + " }, \n" + " {\n" + " \"name\": \"Jerry\", \n" + " \"Grade\": 1, \n" + " \"age\": 10, \n" + " \"gender\": \"M\"\n" + " }\n" + "]"; List<Student> studentList = JSONObject.parseArray(jsonStr, Student.class); System.out.println("studentList:"+studentList); for (int i = 0; i < studentList.size(); i++) { System.out.println("name:"+studentList.get(i).getName()); System.out.println("Grade:"+studentList.get(i).getGender()); System.out.println("age:"+studentList.get(i).getAge()); } }
运行结果:
4.3 复杂嵌套json字符串数组转换为Java对象、list数组
4.3.1 示例1
使用语句:
为4.1和4.2的嵌套使用
示例:
{
“student”: [
{
“name”: “Tom”,
“Grade”: 1,
“age”: 11,
“gender”: “M”
},
{
“name”: “Jerry”,
“Grade”: 1,
“age”: 10,
“gender”: “M”
}
],
“classroom”: {
“class1”: “fifth floor”,
“class2”: “seventh floor”
}
}
首先创建对应的java对象,上面的Student对象可以重复使用,这里需要再添加两个对象,一个整体的包含Student和Classroom的对象School ,一个Classroom对象:
public class School {//这里的Student和Classroom声明为Object类型 private Object Student; private Object Classroom; //省略get、set方法}
public class Classroom { private String class1; private String class2; //省略get、set方法}
进行转换:
public void testJson(){ String jsonStr = "{\n" + " \"student\": [\n" + " {\n" + " \"name\": \"Tom\", \n" + " \"Grade\": 1, \n" + " \"age\": 11, \n" + " \"gender\": \"M\"\n" + " }, \n" + " {\n" + " \"name\": \"Jerry\", \n" + " \"Grade\": 1, \n" + " \"age\": 10, \n" + " \"gender\": \"M\"\n" + " }\n" + " ], \n" + " \"classroom\": {\n" + " \"class1\": \"fifth floor\", \n" + " \"class2\": \"seventh floor\"\n" + " }\n" + "}"; //方法1 调用JSON.parseObject方法将json字符串转换为java对象 School school =JSON.parseObject(jsonStr,School.class); System.out.println("school:"+school); //方法2 先将json字符串转换成JSONObject对象 JSONObject jsonObject = new JSONObject(JSON.parseObject(jsonStr)); //将转换后的JSONObject对象整体转换成School对象 School school1 = (School) JSONObject.toJavaObject(jsonObject,School.class); System.out.println("school1:"+school1); //将School中的student数组转换成list对象 List<Student> studentList = JSONObject.parseArray(school.getStudent().toString(), Student.class); System.out.println("studentList:"+studentList); for (int i = 0; i < studentList.size(); i++) { System.out.print("name:"+studentList.get(i).getName()+","); System.out.print("Grade:"+studentList.get(i).getGender()+","); System.out.println("age:"+studentList.get(i).getAge()); } //将School中的classroom转换成java对象 与刚开始转换school的思路是一样的 //从school对象中getClassroom,返回的是一个Object对象类型 Object classroomObj = school.getClassroom(); //用toString()方法将Object对象转换成String String strClassroom = school.getClassroom().toString(); //方法1 调用JSON.parseObject方法将json字符串转换为java对象 Classroom classroom1 =JSON.parseObject(strClassroom,Classroom.class); System.out.println("classroom1:"+classroom1); //方法2 先将json字符串转换成JSONObject对象 JSONObject jsonClassroom = new JSONObject(JSON.parseObject(strClassroom)); //再利用JSONObject.toJavaObject转换为java对象 Classroom classroom2 = (Classroom) JSONObject.toJavaObject(jsonClassroom,Classroom.class); System.out.println("classroom2:"+classroom2); }
运行结果:
4.3.2 示例2
使用语句:
在4.3.1的基础上新增了两个字段:schoolName和address。Student对象和Classroom对象可以使用,需要重新创建一个整体的对象HighSchool
示例:
{
“schoolName”: “ECNU”,
“address”: “Road”,
“student”: [{
“name”: “Tom”,
“Grade”: 1,
“age”: 11,
“gender”: “M”
},
{
“name”: “Jerry”,
“Grade”: 1,
“age”: 10,
“gender”: “M”
}
],
“classroom”: {
“class1”: “fifth floor”,
“class2”: “seventh floor”
}
}
首先创建对应的java对象,上面的Student对象可以重复使用,这里需要再添加两个对象,一个整体的包含Student和Classroom的对象,一个Classroom对象:
public class School {//这里的Student和Classroom声明为Object类型 private Object Student; private Object Classroom; //省略get、set方法}
public class Classroom { private String class1; private String class2; //省略get、set方法}
进行转换:
public void testJson(){ String jsonStr = "{\n" + " \"schoolName\": \"ECNU\", \n" + " \"address\": \"Road\", \n" + " \"student\": [\n" + " {\n" + " \"name\": \"Tom\", \n" + " \"Grade\": 1, \n" + " \"age\": 11, \n" + " \"gender\": \"M\"\n" + " }, \n" + " {\n" + " \"name\": \"Jerry\", \n" + " \"Grade\": 1, \n" + " \"age\": 10, \n" + " \"gender\": \"M\"\n" + " }\n" + " ], \n" + " \"classroom\": {\n" + " \"class1\": \"fifth floor\", \n" + " \"class2\": \"seventh floor\"\n" + " }\n" + "}"; //方法1 调用JSON.parseObject方法将json字符串转换为java对象 HighSchool highSchool = JSON.parseObject(jsonStr,HighSchool.class); System.out.println("highSchool:"+ highSchool); System.out.println("schoolName:"+ highSchool.getSchoolName()); System.out.println("address:"+ highSchool.getAddress()); //方法2 与之前讲过的一样,这里就省略掉了 //将School中的student数组转换成list对象 List<Student> studentList = JSONObject.parseArray(highSchool.getStudent().toString(), Student.class); System.out.println("studentList:"+studentList); for (int i = 0; i < studentList.size(); i++) { System.out.print("name:"+studentList.get(i).getName()+","); System.out.print("Grade:"+studentList.get(i).getGender()+","); System.out.println("age:"+studentList.get(i).getAge()); } //HighSchool 与刚开始转换school的思路是一样的 //从highSchool对象中getClassroom,返回的是一个Object对象类型 Object classroomObj = highSchool.getClassroom(); //用toString()方法将Object对象转换成String String strClassroom = highSchool.getClassroom().toString(); //方法1 调用JSON.parseObject方法将json字符串转换为java对象 Classroom classroom1 =JSON.parseObject(strClassroom,Classroom.class); System.out.println("classroom1:"+classroom1); //方法2 与之前讲过的一样,这里就省略掉了 }
运行结果:
总结
转换大体上分为两种:
1、以{……}大括号包裹的数据要转换为java对象;
2、 以[ ] 中括号包裹的数据转换为list数组。
首先要明白,以 { } 开头的是JSONObject,以 [ ] 开头的是JSONArray,如果本身就是json字符串的格式(即格式类型为String),那就不用转换,直接使用转换方法进行转换。
但是如果是嵌套字符串的话,从第一次转换后的java对象中取出来的是Object类型,这时候需要先用toString方法转换为String类型,然后再调用方法。