英文:
Parsing of json response from REST API which has id as field name
问题
{
"data":{
"12":{
"value":"$0.00",
"order_id":"12",
"order_date":"2020-08-26 15:50:05",
"category_name":"Games",
"brand_id":"4",
"denomination_name":"AED 50",
"order_quantity":"1",
"vendor_order_id":"A-123",
"vendor_location":"",
"vouchers":{
"804873":{
"pin_code":"41110AE",
"serial_number":"fddfgfgf1234444"
}
}
},
"15":{
"value":"$0.00",
"order_id":"15",
"order_date":"2020-08-26 08:39:11",
"category_name":"Games",
"brand_id":"52",
"brand_name":"PlayStation",
"denomination_name":"$20",
"order_quantity":"1",
"vendor_order_id":"A-316",
"vendor_location":"",
"vouchers":{
"806328":{
"pin_code":"fdfd",
"serial_number":"fawwwww"
}
}
}
}
}
英文:
I want to parse the json string and form a pojo object but the response is somewhat unusual.
I have folloing type of response from API
"data": {
"12": {
"value": "$0.00",
"order_id": "12",
"order_date": "2020-08-26 15:50:05",
"category_name": "Games",
"brand_id": "4",
"denomination_name": "AED 50",
"order_quantity": "1",
"vendor_order_id": "A-123",
"vendor_location": "",
"vouchers": {
"804873": {
"pin_code": "41110AE",
"serial_number": "fddfgfgf1234444"
}
}
},
"15": {
"value": "$0.00",
"order_id": "15",
"order_date": "2020-08-26 08:39:11",
"category_name": "Games",
"brand_id": "52",
"brand_name": "PlayStation",
"denomination_name": "$20",
"order_quantity": "1",
"vendor_order_id": "A-316",
"vendor_location": "",
"vouchers": {
"806328": {
"pin_code": "fdfd",
"serial_number": "fawwwww"
}
}
}
}
}
How do I parse this response since inside data the field name is order id same with voucher
答案1
得分: 1
以下是翻译好的内容:
如果您使用Jackson JSON库,应该拥有如下所示的POJO,并且使用PropertyNamingStrategy.SnakeCaseStrategy
来处理输入JSON中的属性名称:
// 顶层容器
public class Response {
private Map<Integer, Order> data;
// getter/setter
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Order {
private String value; // 可能是某个Currency类
private Integer orderId;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderDate;
private String categoryName;
private Integer brandId;
private String brandName;
private String denominationName; // 也可能是Currency
private Integer orderQuantity;
private String vendorOrderId;
private String vendorLocation;
private Map<Integer, Voucher> vouchers;
// getters/setters
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Voucher {
private String pinCode;
private String serialNumber;
// getters/setters
}
英文:
If you use Jackson JSON library, you should have POJOs like those shown below and use PropertyNamingStrategy.SnakeCaseStrategy
to handle property names in the input JSON:
// top-level container
public class Response {
private Map<Integer, Order> data;
// getter/setter
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Order {
private String value; // may be some Currency class
private Integer orderId;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderDate;
private String categoryName;
private Integer brandId;
private String brandName;
private String denominationName; // may be Currency too
private Integer orderQuantity;
private String vendorOrderId;
private String vendorLocation;
private Map<Integer, Voucher> vouchers;
// getters/setters
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Voucher {
private String pinCode;
private String serialNumber;
// getters/setters
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论