如何将嵌套JSON中的品牌详情解析为BrandDetail实体类
解决方案
你当前的代码无法直接将响应体转换为BrandDetail,因为JSON的根结构并非brandDetail,而是包含metadata和data的外层对象,brandDetail嵌套在data节点下。以下是几种可行的实现方式:
方法一:定义外层DTO类逐层解析
先定义与JSON结构匹配的外层实体类,将整个响应转换为外层对象后,再提取brandDetail:
1. 定义外层实体类
// 外层响应类 public class ApiResponse { private Object metadata; private ProductData data; // Getter和Setter方法 public Object getMetadata() { return metadata; } public void setMetadata(Object metadata) { this.metadata = metadata; } public ProductData getData() { return data; } public void setData(ProductData data) { this.data = data; } } // 对应data节点的类 public class ProductData { private Long productId; private String productName; private BrandDetail brandDetail; // Getter和Setter方法 public Long getProductId() { return productId; } public void setProductId(Long productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public BrandDetail getBrandDetail() { return brandDetail; } public void setBrandDetail(BrandDetail brandDetail) { this.brandDetail = brandDetail; } } // 你的BrandDetail实体类(确保字段与JSON匹配) public class BrandDetail { private Long brandId; private String brandName; private String brandCode; // Getter、Setter方法,以及必要的构造函数 public Long getBrandId() { return brandId; } public void setBrandId(Long brandId) { this.brandId = brandId; } public String getBrandName() { return brandName; } public void setBrandName(String brandName) { this.brandName = brandName; } public String getBrandCode() { return brandCode; } public void setBrandCode(String brandCode) { this.brandCode = brandCode; } }
2. 修改解析代码
HttpGet httpGet = buildHttpGet("/externalApiURL"); HttpResponse response = getHttpClient().execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) { ObjectMapper objectMapper = new ObjectMapper(); // 忽略未知字段,避免JSON中多余字段导致解析失败 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ApiResponse apiResponse = objectMapper.readValue(entity.getContent(), ApiResponse.class); BrandDetail brandDetail = apiResponse.getData().getBrandDetail(); }
方法二:使用Jackson树模型直接提取节点
无需定义外层DTO,通过JsonNode逐层导航到目标节点再转换:
HttpGet httpGet = buildHttpGet("/externalApiURL"); HttpResponse response = getHttpClient().execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) { ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(entity.getContent()); // 从根节点逐层获取到brandDetail节点 JsonNode brandDetailNode = rootNode.path("data").path("brandDetail"); BrandDetail brandDetail = objectMapper.treeToValue(brandDetailNode, BrandDetail.class); }
方法三:使用JsonPath直接提取片段
需要先引入JsonPath依赖(如com.jayway.jsonpath:json-path),然后直接定位到brandDetail节点:
HttpGet httpGet = buildHttpGet("/externalApiURL"); HttpResponse response = getHttpClient().execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) { String jsonContent = EntityUtils.toString(entity); // 用JsonPath提取brandDetail的JSON片段 String brandDetailJson = JsonPath.read(jsonContent, "$.data.brandDetail").toString(); ObjectMapper objectMapper = new ObjectMapper(); BrandDetail brandDetail = objectMapper.readValue(brandDetailJson, BrandDetail.class); }
内容的提问来源于stack exchange,提问作者user3919727
相关产品推荐
相关产品推荐

