← 返回文章列表JSON 反序列化详解
javajson
嵌套 JSON 结构设计、DTO 分层与 Jackson 反序列化映射。
JSON结构
{
"code": 200,
"msg": "success",
"result": {
"list": [
{ "lowprice": 1035.1, "highprice": 1049.9, ..., "code": "au9999" },
...
]
}
}
DTO设计
@Data
public class GoldPriceApi {
private String code;
private String msg;
private ResultData result;
@Data
public static class ResultData {
private List<PriceData> list;
}
}
阿里巴巴FastJson
List<PriceData> list = JSON.parseObject(str, GoldPriceApi.class, JSONReader.Feature.SupportSmartMatch)
.getResult()
.getList();
Spring boot 原生Jackson
JsonMapper mapper = JsonMapper.builder()
.propertyNamingStrategy(PropertyNamingStrategies.LOWER_CASE)
.build();
List<PriceData> list = mapper.readValue(str, GoldPriceApi.class)
.getResult()
.getList();