如何将嵌套JSON响应解析为Java对象列表
解析嵌套JSON为Java对象列表的解决方案
没问题,我帮你一步步搞定这个嵌套JSON的解析需求!因为你的JSON响应是外层对象包裹着数组结构,所以我们需要两个类:一个对应数组里的Country实体,另一个是外层的响应包装类。
1. 完善你的POJO类
首先,确认你的Country类要和JSON里的字段一一对应,记得加上无参构造和Getter/Setter方法(序列化/反序列化必须依赖这些):
public class Country { // 字段名和JSON键完全匹配,大小写也要一致 private String Name; private String CurrencyCode; private String CurrencyName; // 必须保留无参构造函数 public Country() {} // Getter和Setter方法 public String getName() { return Name; } public void setName(String name) { Name = name; } public String getCurrencyCode() { return CurrencyCode; } public void setCurrencyCode(String currencyCode) { CurrencyCode = currencyCode; } public String getCurrencyName() { return CurrencyName; } public void setCurrencyName(String currencyName) { CurrencyName = currencyName; } }
然后创建外层响应包装类,用来对应JSON最外层的三个字段:
import java.util.List; public class CountryResponse { private boolean IsSuccess; private int TotalCount; private List<Country> Response; public CountryResponse() {} // Getter和Setter方法 public boolean isSuccess() { return IsSuccess; } public void setSuccess(boolean success) { IsSuccess = success; } public int getTotalCount() { return TotalCount; } public void setTotalCount(int totalCount) { TotalCount = totalCount; } public List<Country> getResponse() { return Response; } public void setResponse(List<Country> response) { Response = response; } }
2. 用Jackson解析(Spring Boot首选)
如果是Spring Boot项目,Jackson已经默认集成了,直接使用即可;普通Java项目需要先添加Maven依赖:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> <!-- 建议使用最新稳定版 --> </dependency>
解析代码示例:
import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonParserDemo { public static void main(String[] args) { // 你的JSON响应字符串 String jsonStr = "{ \"IsSuccess\": true, \"TotalCount\": 250, \"Response\": [ { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" }, { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" }, { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" } ] }"; ObjectMapper objectMapper = new ObjectMapper(); try { // 将JSON字符串转成外层响应对象 CountryResponse response = objectMapper.readValue(jsonStr, CountryResponse.class); // 取出Country列表 List<Country> countryList = response.getResponse(); // 测试遍历输出 for (Country country : countryList) { System.out.printf("国家:%s,货币代码:%s,货币名称:%s%n", country.getName(), country.getCurrencyCode(), country.getCurrencyName()); } } catch (Exception e) { e.printStackTrace(); } } }
3. 用Gson解析(Google官方库)
如果习惯使用Gson,先添加Maven依赖:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> <!-- 最新稳定版 --> </dependency>
解析代码示例:
import com.google.gson.Gson; public class GsonParserDemo { public static void main(String[] args) { String jsonStr = "{ \"IsSuccess\": true, \"TotalCount\": 250, \"Response\": [ { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" }, { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" }, { \"Name\": \"Afghanistan\", \"CurrencyCode\": \"AFN\", \"CurrencyName\": \"Afghan afghani\" } ] }"; Gson gson = new Gson(); // 直接将JSON转成外层响应对象 CountryResponse response = gson.fromJson(jsonStr, CountryResponse.class); List<Country> countryList = response.getResponse(); // 测试输出 countryList.forEach(country -> System.out.println(country.getName() + " - " + country.getCurrencyName())); } }
关键注意点
- 字段名要和JSON的键大小写完全匹配,如果想改成驼峰命名(比如把POJO字段改成
isSuccess),可以用注解映射:Jackson用@JsonProperty("IsSuccess"),Gson用@SerializedName("IsSuccess")。 - 必须保留无参构造函数,否则序列化库无法实例化对象。
- 如果JSON里存在未定义的可选字段,可以用
@JsonIgnoreProperties(ignoreUnknown = true)(Jackson)或者Gson的宽松模式来避免解析报错。
内容的提问来源于stack exchange,提问作者Andulos
相关产品推荐
相关产品推荐

