如何在Java中使用Gson解析含billingInformation的嵌套JSON文件
用Gson解析带嵌套对象的JSON
方案一:手动逐层解析(适配现有代码逻辑)
不需要移除billingInformation字段,只需先从根JSON对象中提取该字段对应的子对象,再逐层解析嵌套内容:
Reader reader = new BufferedReader(Files.newBufferedReader(Paths.get(bill))); // 获取根JSON对象 JsonObject rootObj = JsonParser.parseReader(reader).getAsJsonObject(); // 提取billingInformation子对象 JsonObject billingInfoObj = rootObj.getAsJsonObject("billingInformation"); // 解析Taxes Taxes taxes = new Taxes(); JsonObject taxesObj = billingInfoObj.getAsJsonObject("taxes"); taxes.setGST(taxesObj.get("gst").getAsDouble()); taxes.setHST(taxesObj.get("hst").getAsDouble()); // 解析billTo BillTo billTo = new BillTo(); JsonObject billToObj = billingInfoObj.getAsJsonObject("billTo"); billTo.setName(billToObj.get("name").getAsString()); billTo.setAddress(billToObj.get("address").getAsString()); billTo.setPhoneNumber(billToObj.get("phoneNumber").getAsString()); // 解析salesAgent SalesAgent salesAgent = new SalesAgent(); JsonObject agentObj = billingInfoObj.getAsJsonObject("salesAgent"); salesAgent.setName(agentObj.get("name").getAsString()); salesAgent.setAgentCode(agentObj.get("agentCode").getAsInt()); // 解析items及嵌套的item列表 Items items = new Items(); JsonObject itemsObj = billingInfoObj.getAsJsonObject("items"); items.setCurrency(itemsObj.get("currency").getAsString()); JsonArray itemArray = itemsObj.getAsJsonArray("item"); List<Item> itemList = new ArrayList<>(); for (JsonElement elem : itemArray) { JsonObject itemObj = elem.getAsJsonObject(); Item item = new Item(); item.setHsnCode(itemObj.get("hsnCode").getAsString()); item.setDescription(itemObj.get("description").getAsString()); item.setOriginCountry(itemObj.get("originCountry").getAsString()); item.setQuantity(itemObj.get("quantity").getAsInt()); item.setUnitPrice(itemObj.get("unitPrice").getAsDouble()); itemList.add(item); } items.setItem(itemList);
方案二:实体类自动映射(推荐,简洁高效)
定义与JSON结构完全对应的实体类,利用Gson的自动反序列化功能,无需手动逐层解析:
1. 定义所有嵌套实体类
根实体类 BillRoot
public class BillRoot { private BillingInformation billingInformation; // Getter & Setter public BillingInformation getBillingInformation() { return billingInformation; } public void setBillingInformation(BillingInformation billingInformation) { this.billingInformation = billingInformation; } }
BillingInformation 类
public class BillingInformation { private Taxes taxes; private BillTo billTo; private SalesAgent salesAgent; private Items items; // Getter & Setter 需为每个字段生成 }
Taxes 类
public class Taxes { private double gst; private double hst; // Getter & Setter public double getGst() { return gst; } public void setGst(double gst) { this.gst = gst; } public double getHst() { return hst; } public void setHst(double hst) { this.hst = hst; } }
BillTo 类
public class BillTo { private String name; private String address; private String phoneNumber; // Getter & Setter }
SalesAgent 类
public class SalesAgent { private String name; private int agentCode; // Getter & Setter }
Items 类
public class Items { private List<Item> item; private String currency; // Getter & Setter }
Item 类
public class Item { private String hsnCode; private String description; private String originCountry; private int quantity; private double unitPrice; // Getter & Setter }
2. 自动反序列化代码
Gson gson = new Gson(); Reader reader = new BufferedReader(Files.newBufferedReader(Paths.get(bill))); // 直接将JSON解析为根实体类对象 BillRoot billRoot = gson.fromJson(reader, BillRoot.class); // 直接获取各层级数据 BillingInformation billingInfo = billRoot.getBillingInformation(); Taxes taxes = billingInfo.getTaxes(); BillTo billTo = billingInfo.getBillTo(); // 其他数据可通过对应getter方法获取
注意事项
- 实体类字段名需与JSON中的键大小写完全匹配,若需自定义字段名,可使用
@SerializedName("JSON键名")注解指定 - 字段若可能为null,建议使用包装类(如
Double代替double)避免空指针异常 - 所有实体类需提供默认无参构造方法(Gson依赖此实例化对象)
内容的提问来源于stack exchange,提问作者user19204430
相关产品推荐
相关产品推荐

