简体   繁体   中英

Parsing of json response from REST API which has id as field name

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM