
JSON解析方案及json转Map反序列号,应用于支付回调通知中接收参数
1.使用 Jackson:业界标配 2.使用 Gson:轻量好用 3.使用 FastJSON:高性能
class User {
@Expose(serialize = true)
private int id;
@Expose
private String name;
@Expose
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") //Jackson 日期格式化
private Date birthday;
@JsonIgnore //Jackson 忽略字段
private String sex;
@Expose
@JsonProperty("city_name") //Jackson 重命名字段
private String cityName;
@Expose(serialize = false) //gson 忽略字段
private String sex2;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", sex2='" + sex2 + '\'' +
", cityName='" + cityName + '\'' +
'}';
}
public String getSex2() {
return sex2;
}
public void setSex2(String sex2) {
this.sex2 = sex2;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
// Getters 和 Setters
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;
}
}1. 使用 Jackson:业界标配
package com.example.core.mydemo.json2025;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.annotations.Expose;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Date;
import java.util.Map;
public class JacksonExample {
public static void main(String[] args) throws Exception {
//(through reference chain: com.example.core.mydemo.json2025.User["cityName"])
String json = "{\"id\":1,\"name\":\"张三\",\"birthday\":\"2025-03-04 10:00:00\",\"sex\":\"男\",\"city_name\":\"北京\"}";
ObjectMapper objectMapper = new ObjectMapper();
/**
* JSON 转对象(反序列化)
*/
User user = objectMapper.readValue(json, User.class);
System.out.println(user.toString()); // 输出:User{id=1, name='张三', birthday=Tue Mar 04 18:00:00 CST 2025, sex='null', cityName='北京'}
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
System.out.println("map=" + map); // 输出: map={id=1, name=张三, birthday=2025-03-04 10:00:00, sex=男, city_name=北京}
/**
* 对象转 JSON(序列化)
*/
user = new User();
user.setId(1);
user.setName("李四");
//测试注解高级功能
user.setBirthday(new Date());
user.setSex("男");
user.setCityName("上海");
json = objectMapper.writeValueAsString(user);
System.out.println(json); // 输出:{"id":1,"name":"李四","birthday":"2025-03-05","city_name":"上海"}
}
}2.使用 Gson:轻量好用
package com.example.core.mydemo.json2025;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Date;
import java.util.Map;
public class GsonExample {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"王五\"}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user.getName()); // 输出:王五
/**
* 反序列化
*/
Map map = gson.fromJson(json, Map.class);
System.out.println("map=" + map); // 输出: map={id=1.0, name=王五}
user = new User();
user.setId(2);
user.setName("赵六");
//测试注解高级功能
user.setBirthday(new Date());
user.setSex2("男");
user.setCityName("上海");
/**
* 序列化
*/
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation(); //配合@Expose使用,其他的字段也都要加上该注释,默认是true
Gson gson3 = builder.create();
json = gson3.toJson(user);
System.out.println(json); // 输出:{"id":2,"name":"赵六"}
}
}3.使用 FastJSON:高性能
package com.example.core.mydemo.json2025;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class FastJsonExample {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"小明\"}";
User user = JSON.parseObject(json, User.class);
System.out.println(user.getName()); // 输出:小明
/**
* 反序列化
*/
Map<String, Object> map = JSON.parseObject(json, Map.class);
System.out.println("map=" + map); //输出: map={name=小明, id=1} 可以用于支付的异步通知接收消息
user = new User();
user.setId(3);
user.setName("小红");
//测试注解高级功能
user.setBirthday(new Date());
user.setSex2("男");
user.setCityName("上海");
//默认
json = JSON.toJSONString(user);
System.out.println(json); //输出: {"birthday":1741164892016,"cityName":"上海","id":3,"name":"小红","sex2":"男"}
//日期格式 序列化
// 第一种方法:
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
json = JSON.toJSONString(user,SerializerFeature.WriteDateUseDateFormat);
System.out.println(json); // 输出: {"birthday":"2025-03-05","cityName":"上海","id":3,"name":"小红","sex2":"男"}
// 第二种方法:
json = JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(json); //输出: {"birthday":"2025-03-05 16:54:52.016","cityName":"上海","id":3,"name":"小红","sex2":"男"}
}
}