JSON解析器:
常见的解析器:Jsonlib,Gson,fastjson,jackson
最好使用最新jar包(百度搜索Gson然后去GitHub上找,或者Maven上也有)
1. JSON转为Java对象
1.引入JAR包
2. 创建Gson对象
调用" Object obj = g.fromJson(JSON字符串,对象类型.class) ";进行转换
创建一个Student类:
public class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
实例:
public static void main(String[] args) {
//1. 创建Gson对象
Gson g = new Gson();
//2. 转换
String str = "{\"id\":1,\"name\":\"马保国\",\"age\":56}\n";
Student student = g.fromJson(str, Student.class);
System.out.println(student);
System.out.println("---------------------------");
//3.集合转换
String str2 = "{\"id\":1,\"name\":\"马保国\",\"age\":56,\"page\":[\"hello\",\"java\",\"hahaha\"]}\n";
HashMap data = g.fromJson(str2, HashMap.class);
List page = (List) data.get("page");//JSON中的数组转出来是ArrayList
System.out.println(page.get(1));
}
运行结果:
Student{id=1, name='马保国', age=56}
----------------------------------
java
2.Java对象转换JSON
1.引入JAR包
2. 创建Gson对象
3.调用" String json = g.toJSON(要转换的对象) "进行转换
实例:
public static void main(String[] args) {
//1. 创建Gson对象
Gson g = new Gson();
//2. 转换
Student student1 = new Student(1,"马保国",56);
String str = g.toJson(student1);
System.out.println(str);
System.out.println("--------------------------------------------");
//3. 多个对象转换
Student student2 = new Student(2,"卢本伟",28);
Student student3 = new Student(3,"蔡徐坤",15);
ArrayList<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
String str2 = g.toJson(students);
System.out.println(str2);
}
运行结果:
{"id":1,"name":"马保国","age":56}
--------------------------------------------
[{"id":1,"name":"马保国","age":56},{"id":2,"name":"卢本伟","age":28},{"id":3,"name":"蔡徐坤","age":15}]
最好使用最新jar包(百度搜索Gson然后去GitHub上找,或者Maven上也有)
1. JSON转为Java对象
1.引入JAR包
2.在需要转换Java对象的位置, 编写如下代码:
实例:
public class Demo4 {
public static void main(String[] args) {
String str = "{\"id\":1,\"name\":\"马保国\",\"age\":56}";
Student student = JSON.parseObject(str, Student.class);
System.out.println(student);
}
}
运行结果:
Student{id=1, name='马保国', age=56}
2.Java对象转换JSON
1.引入JAR包
2.在需要转换JSON字符串的位置编写如下代码即可:
实例:
public static void main(String[] args) {
Student student1 = new Student(1,"马保国",56);
String str = JSON.toJSONString(student1);
System.out.println(str.length());
}
运行结果:
{"id":1,"name":"马保国","age":56}
注意:
FastJson是靠对象属性的get、Set方法生成josn文件的,没有get、set方法无法生成
1. JSON转为Java对象
1.导入jackson的相关jar包
2.创建Jackson核心对象 ObjectMapper
3.调用ObjectMapper的相关方法进行转换
2. Java对象转换JSON
使用步骤:
1.导入jackson的相关jar包
2.创建Jackson核心对象 ObjectMapper
3.调用ObjectMapper的相关方法进行转换
1.转换方法:
writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
writeValueAsString(obj):将对象转为json字符串
2.注解:
3.复杂java对象转换