建立实体类
user
package com.jock168.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.time.LocalDate;
// 用于指定生成JSON串的属性顺序
@JsonPropertyOrder({"age", "user_name"})
public class User {
// 重命名属性的注解
@JsonProperty("user_name")
private String name;
@JsonProperty("age")
private int age;
// 指定日期格式的注解
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private LocalDate birth;
// 指定在JSON串中不生成此属性
@JsonIgnore
private String gender;
public User() {
}
public User(String name, int age, LocalDate birth) {
this.name = name;
this.age = age;
this.birth = birth;
}
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 LocalDate getBirth() {
return birth;
}
public void setBirth(LocalDate birth) {
this.birth = birth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
", birth=" + birth +
'}';
}
}
student
package com.open.entity;
public class Student {
private int id;
private String name;
private int age;
private String gender;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
'}';
}
public Student() {
}
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
使用java-json实现json格式转字符串和字符串转json
package com.open.test;
import com.open.entity.Student;
import org.json.JSONObject;
import org.junit.Test;
public class JsonTest {
@Test
public void testJsonToStudent() {
// 1. 提供要转换的 JSON 字符串
String json = "{"gender":"男","name":"张三","id":1,"age":20}";
// 2. 创建 JSonObject 对象
JSONObject object = new JSONObject(json);
// 3. 调用 get(String key)
Student student = new Student();
student.setId(object.getInt("id"));
student.setName(object.getString("name"));
student.setAge(object.getInt("age"));
student.setGender(object.getString("gender"));
System.out.println(student);
}
@Test
public void testStudentToJson() {
// 把 Student 对象转换为 JSON 字符串
Student student = new Student(1, "张三", 20, "男");
// JSON-java 中的方法
// 要想使用 JSON官网提供的解析JSON工具,必须先创建 JSONObject对象
JSONObject object = new JSONObject(student);
// 调用 JSonObject 对象的 toString() 方法就会生成JSON字符串了
String json = object.toString();
System.out.println(json);
}
}
gson
package com.xianopen.test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.xianopen.entity.Student;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GsonTest {
@Test
public void testJsonToMap() {
String json = "{"1":{"id":1,"name":"张三","age":20},"2":{"id":2,"name":"李四","age":21},"3":{"id":3,"name":"王五","age":22},"4":{"id":4,"name":"赵六","age":23}}";
Gson gson = new Gson();
Map map = gson.fromJson(json, new TypeToken
fastjson
package com.jock168.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jock168.entity.User;
import org.junit.Test;
import java.util.*;
public class FastJsonTest {
@Test
public void testMapToJSON() {
Map map = new HashMap<>();
map.put(1, new User("张三", 18, new Date()));
map.put(2, new User("李四", 20, new Date()));
// 序列化
String json = JSON.toJSONString(map);
System.out.println(json);
// 反序列化
Map users = JSON.parseObject(json, new TypeReference
jackson
package com.jock168.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.jock168.entity.User;
import org.junit.Test;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JacksonTest {
@Test
public void testMapToJSON() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
Map users = new HashMap<>();
User user = new User();
user.setName("张三");
user.setAge(20);
user.setBirth(LocalDate.now());
users.put(1, user);
user = new User();
user.setName("李四");
user.setAge(21);
user.setBirth(LocalDate.now());
users.put(2, user);
// 序列化
String json = mapper.writevalueAsString(users);
System.out.println(json);
// 反序列化
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, Integer.class, User.class);
Map userMap = mapper.readValue(json, mapType);
//Map userMap = mapper.readValue(json, new TypeReference