【问题标题】:Jackson- How to process the request data passed as JSON (Netsed Json)?Jackson-如何处理作为 JSON (Netsed Json) 传递的请求数据?
【发布时间】:2021-06-21 22:33:07
【问题描述】:
{
    "DistributionOrderId" : "Dist_id_1",
    "oLPN": 
    {
        "Allocation": 
        {
            "AID": "12345"      
        },
        "Allocation": 
        {
            "AID": "123456" ,
            "SerialNbr": "SRL001",
            "BatchNbr": "LOT001"
            "RevisionNbr": "RVNBR1"
        }
    },
    "oLPN": 
    {
        "Allocation": 
        {
            "AID": "12123"      
        }
        "Allocation": 
        {
            "AID": "12124"      
        }
    }
}

我有一个来自供应商的 JSON 请求,如何将值存储为 Java POJO 并进一步使用它们? 编辑:向 JSON 添加属性

【问题讨论】:

    标签: java json json-deserialization jackson-databind nsjsonserialization


    【解决方案1】:

    由于您没有使用杰克逊数据绑定中常见的驼峰式约定,我建议您使用适当的注释定义一个 java 对象。该对象当前不是有效的 json。例如,如果 json 看起来像这样:

    {
        "DistributionOrderId" : "Dist_id_1",
        "oLPN": [ ... ]
    }
    

    数组中的每个oLPN 都是这样的对象:

    {
        "Allocation": [{ "AID": ... }, { "AID": ... }, ... ]
    }
    

    你可以为配送订单写一个类

    public class DistributionOrder {
        @JsonProperty("DistributionOrderId") private String id;
        @JsonProperty("oLPN") private List<OLPN> olpn;
        // getters and setters
    }
    

    然后另一个用于 OLPN 对象

    public class OLPN {
        @JsonProperty("Allocation") private String allocation;
        @JsonProperty("AID") private String aid;
        // getters and setters
    }
    

    然后您可以根据需要使用对象映射器。例如

    ObjectMapper mapper = ... // get your object mapper from somewhere
    
    DistributionOrder distributionOrder = mapper.readValue(raw, DistributionOrder.class);
    

    另见object mapper javadoc

    【讨论】:

    • 我在分配下的属性很少,例如“SerialNbr”、“BatchNbr”、“RevisionNbr”,它们是可选字段。此外,Olpn 对象的“权重”和“uom”属性也是强制属性。这种情况下如何重构 JSON 请求?
    【解决方案2】:

    json key 不能重复,带有 value 的重复 key 会被丢弃。 我已经格式化了你的 json 文本,可能是下面的表达式:

    {
        "DistributionOrderId" : "Dist_id_1",
        "oLPN": 
        [
          {
            "Allocation": 
              [
                {
                  "AID": "123456" ,
                  "SerialNbr": "SRL001",
                  "BatchNbr": "LOT001",
                  "RevisionNbr": "RVNBR1"
                },
                {
                  "AID": "12345"
                }
              ] 
          },
          {
              "Allocation": 
              [
                {
                    "AID": "12123"      
                },
                {
                    "AID": "12124"      
                }
              ]
          }
        ]
    }
    

    这个结构匹配java对象是

    class DistributionOrder{
      @JsonProperty("DistributionOrderId")
      String distributionOrderId;
      List<OLPN> oLPN;
    }
    
    class OLPN {
      @JsonProperty("Allocation")
      List<Allocation> allocation;
    }
    
    class Allocation{
      String AID;
      @JsonProperty("SerialNbr")
      String serialNbr;
      @JsonProperty("BatchNbr")
      String batchNbr;
      @JsonProperty("RevisionNbr")
      String revisionNbr;
    }
    

    【讨论】:

    • 在我的情况下,我在 Json 请求中传递了多个 oLPN 对象,并且每个 olpn 对象都将具有强制权重和 uom 属性。并且每个 Allocation 对象将具有可选的“SerialNbr”:“”、“BatchNbr”:“”“RevisionNbr”:“”属性。在这种情况下,正确的 JSON 格式是什么?
    • 也许你可以发送一个最复杂的示例文本,json类型可以扩展,任何你需要的@HarshithRao
    • 你发送的意思是 oLPN 和 Allocation 两个数组,我要再说一遍:并且 json 字符串不能在同一级别保存相同的键名。根据您的需要,我已经更改了我的答案,如果您也有问题,请回复! @HarshithRao
    • 是的,我明白了.. serialNbr、batchNbr、revisionNbr 不会总是在分配数组中传递。那么在我的 Allocation POJO 类中,应该使用哪个 json 注释呢? ``` @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public class Allocation { private String AID;私有字符串 SerialNbr;私有字符串 BatchNbr;私有字符串修订版Nbr; ```上面会起作用吗?
    • 如果你想要json文本到pojo,pojo必须有json文本中存在的所有字段,如果你想要pojo到json文本,@JsonInclude(JsonInclude.Include.NON_NULL)可以帮助过滤null值,仅供参考,如果你理解,请选择我的答案,如果没有,请评论关注。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2020-08-07
    • 2018-05-18
    相关资源
    最近更新 更多