大家好,我在使用 Gson 反序列化json时遇到了一些麻烦。 我通过一个简单的例子向您展示我的问题:
这是我收到的JSON:
"order": {
"id": "123",
"name": "BOBI",
"items": {
"totalSize": 2,
"records": [
{
"id": "456",
"Quantity": 1.0,
"Description": "Shipping",
"TotalAmount": 6.29,
"TotalTaxAmount": 0.3,
"UnitPrice": 5.99
}
]
}
}
我想删除/跳过这个无用的部分:
"totalSize": 2,
"records": [
我需要像 @SerializedName(“ items.records”)这样的功能,才能通过使用如下所示的Java类来跳过一个级别:
public class SFOrderDto {
private String id;
private String name;
@SerializedName("items.records")
private List<SFProductDto> records;
}
主要电话:
SFOrderDto orderArray = gson.fromJson(jsonOrder, SFOrderDto.class);
希望有人能帮助我,对不起我的英语不好! :)
答案 0 :(得分:0)
我在这里找到了解决方法:Use jackson annotation JsonUnwrapped on a field with name different from its getter
添加@JsonUnwrapped解决了我的问题。
public class SFOrderDto {
private String id;
private String name;
@JsonUnwrapped
@SerializedName("items.records")
private List<SFProductDto> records;
}
感谢@papaya和@Michal Ziober试图帮助我:)