【发布时间】:2020-01-09 21:51:24
【问题描述】:
我正在尝试解析一些看起来像这样的 json 数据:
{
"store_name": "Coffee Co",
"location": "New York",
"supplier_name": "Cups Corps",
"supplier_id": 12312521,
"supplier_email": "cups@cups.net"
}
这是我的 Java POJO
class Store {
@com.google.gson.annotations.SerializedName("store_name")
String storeName;
String location;
Supplier supplier;
}
class Supplier {
String id;
String name;
String email;
}
//Getters and setters omitted
我遇到的问题是Supplier 的字段被直接展平到Store 记录中。我尝试为Supplier 添加一个TypeAdapter 到我的Gson 对象,但它没有被触发,因为传入的json 对象上没有名为supplier 的字段。我也不能为 supplier 使用备用名称,因为它需要来自所有三个字段的信息才能创建。
解析此数据以便也可以填充嵌套的Supplier 字段的最佳方法是什么?
【问题讨论】:
标签: json gson deserialization json-deserialization