参考资料地址: https://blog.csdn.net/qq_41143240/article/details/115671561
使用JsonProperty注解
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.HashMap;
/**
* 用驼峰的实体类接受命名不规范的响应参数
*
* @author lyn
* @date 2023/4/10 19:03
*/
public class JsonPropertyTest {
@Data
public class SmellObj {
@JsonProperty("order_status")
private String orderStatus;
@JsonProperty("order_id")
private Long orderId;
}
public static void main(String[] args) {
//模拟第三方的响应参数
HashMap<String, Object> map = new HashMap<>(2);
map.put("order_status","支付中");
map.put("order_id",1000L);
String json = JSON.toJSONString(map);
//模拟接受参数
SmellObj obj = JSON.parseObject(json, SmellObj.class);
//查看结果
System.out.println(obj);
//SmellObj(orderStatus=支付中, orderId=1000)
json = JSON.toJSONString(obj);
System.out.println(json);
//{"orderId":1000,"orderStatus":"支付中"}
}
}
标签:JsonProperty,实体类,驼峰,map,SmellObj,响应,JSON,import,order
From: https://www.cnblogs.com/lyn8100/p/17304201.html