Java中java对象和JSON字符串的转换
1.如何实现转换?
最原始的方法便是对JSON提交过来的字符串进行切割,直到找到自己所需要的部分,此方法可以实现,但是代码量太过冗余,我们可以使用JSON解析器实现这些工作,接下来给大家介绍几种常用的JSON解析器:
1.Jsonlib
2.Gson
3.fastjson
4.jackson
下面我将用jackson为大家演示
jsckson使用前必须创建目录lib导入jsonjar包
并进行依赖注入(as a library)
最后还要介绍一款插件GsonFormat
我们在将JSON字符串转换为Java对象的时候,首先得创建对应的类
而GsonFormat可以帮我们简化工作量,直接将JSON字符串复制进去便会创建相应的对象
2.JSON对象转换为字符串
package com.my.json2;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
public class Person {
private String name;
@JsonIgnore
private int age;
private char sex;
@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8")
private Date birthday;
public Person(String name, int age, char sex, Date birthday) {
this.name = name;
this.age = age;
this.sex = sex;
this.birthday = birthday;
}
public Person() {
}
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 char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package com.my.json2;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;
public class Text {
public static void main(String[] args) throws JsonProcessingException {
//通过有参构造创建对象
Person person=new Person("Ls",18,'男',new Date());
//创建Jackson对象
ObjectMapper objectMapper = new ObjectMapper();
//调用方法将对象转换为字符串
String s = objectMapper.writeValueAsString(person);
System.out.println(s);
}
}
//Date上未加注释的输出结果
//{"name":"Ls","age":18,"sex":"男","birthday":1600912698623}
//Date加 @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8")
//{"name":"Ls","age":18,"sex":"男","birthday":"2020-09-24 10-02-36"}
//age上加@JSonIgnore以及 @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8")后的输出结果
//{"name":"Ls","sex":"男","birthday":"2020-09-24 10-19-02"}
//我在这里分别技师一下两个主食的用途
//@JsonIgnore的意思是在对象转换为字符串的时候忽略此成员变量,不参与转换,类似于序列化流中的transient,不参与序列化
// @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss",timezone = "GMT+8") 的意思是规定时间格式
//pattern中写的是时间格式,等同于simpleDateFormat中的格式,timezone中指的是默认时区+8,因为我们处于东八区
//在不同时区+不同的数字
3.JSON字符串转换为Java对象
package com.my.json3;
public class Teacher {
private String name;
private int age;
private String sex;
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
package com.my.json3;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Text {
public static void main(String[] args) throws IOException {
//{"name":"zz","age":18,"sex":"男"}
//自己构造了一个JSON字符串
String JSONStr="{\"name\":\"zz\",\"age\":18,\"sex\":\"男\"}";
//创建对象
ObjectMapper objectMapper = new ObjectMapper();
//调用方法将字符串写入到指定的对象中
Teacher teacher = objectMapper.readValue(JSONStr, Teacher.class);
System.out.println(teacher);
//输出结果
//Teacher{name='zz', age=18, sex='男'}
}
}
4.Java对象转换为JSON数组
package com.my.json4;
public class Car {
private String brand;
private double price;
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
public Car() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.my.json4;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
public class Text {
public static void main(String[] args) throws JsonProcessingException {
//以下价格单位为万
//创建汽车对象
Car car1 = new Car("布加迪", 4000);
Car car2 = new Car("奔驰s", 250);
Car car3 = new Car("科尼塞克", 4000);
Car car4 = new Car("兰博基尼", 400);
Car car5 = new Car("法拉利", 400);
//创建集合,并将每个对象添加入集合
List<Car> list=new ArrayList<>();
list.add(car1);
list.add(car2);
list.add(car3);
list.add(car4);
list.add(car5);
//创建Jackson对象
ObjectMapper objectMapper = new ObjectMapper();
//将list集合转换为字符串
String s = objectMapper.writeValueAsString(list);
System.out.println(s);
//[{"brand":"布加迪","price":4000.0},{"brand":"奔驰s","price":250.0},{"brand":"科尼塞克","price":4000.0},{"brand":"兰博基尼","price":400.0},{"brand":"法拉利","price":400.0}]
}
}
5.Map集合转换为JSON字符串
package com.my.json5;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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;
}
}
package com.my.json5;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public class Text {
public static void main(String[] args) throws JsonProcessingException {
//创建学生对象
Student s1 = new Student("ls", 18);
Student s2 = new Student("zz", 23);
Student s3 = new Student("yy", 33);
//创建map集合并向其中添加数据
Map<Integer,Student> map=new HashMap<>();
map.put(1,s1);
map.put(2,s2);
map.put(3,s3);
//创建JackSon对象
ObjectMapper objectMapper = new ObjectMapper();
//转换
String s = objectMapper.writeValueAsString(map);
System.out.println(s);
//输出结果
//{"1":{"name":"ls","age":18},"2":{"name":"zz","age":23},"3":{"name":"yy","age":33}}
}
}