解析来自具有字段名为“id”的 REST API 的 JSON 响应。

huangapple go评论79阅读模式
英文:

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&lt;Integer, Order&gt; data;
    // getter/setter
}

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Order {
    private String value; // may be some Currency class
    private Integer orderId;

    @JsonFormat(pattern = &quot;yyyy-MM-dd HH:mm:ss&quot;)
    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&lt;Integer, Voucher&gt; vouchers;

    // getters/setters
}

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Voucher {
    private String pinCode;
    private String serialNumber;
    
    // getters/setters
}

huangapple
  • 本文由 发表于 2020年8月31日 00:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63659833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定