【问题标题】:Extract parameters from nested Json从嵌套的 Json 中提取参数
【发布时间】:2019-08-26 17:09:26
【问题描述】:

我有一个 json 字符串,如下所示:

{
    \"request\": {
        \"requestId\": \"dd92f43ec593d2d8db94193b7509f5cd\",
        \"notificationType\": \"EntityAttribute\",
        \"notificationSource\": \"ODS\"
    },
    \"entityattribute\": {
        \"entityId\": \"123\",
        \"attributeType\": \"DATE_OF_BIRTH\"
    }
}

我想反序列化实体属性到一个对象:

public class EntityAttributeNotification {
    private String attributeType;
    private String entityId;
}

一种方法是先使用json路径(即entityattribute/entityId)提取entityId和attributeType,然后创建一个对象EntityAttributeNotification。

我想知道有没有办法直接反序列化entityattribute到EntityAttributeNotification。 我也尝试过使用 JsonMixin 注释,但这不适用于此处。

【问题讨论】:

    标签: json objectmapper


    【解决方案1】:

    通过下面的方法可以提取嵌套JSON的ParametersValues

    const object1 ={
        "request": {
            "requestId": "dd92f43ec593d2d8db94193b7509f5cd",
            "notificationType": "EntityAttribute",
            "notificationSource": "ODS"
        },
       "entityattribute": {
            "entityId": "123",
            "attributeType": "DATE_OF_BIRTH"
        }
    };
    var keys = [];
    for (let [key, value] of Object.entries(object1)) {
        if(typeof value == 'object'){
            keys.push(key);
            for (let [key1, value1] of Object.entries(value)) {
                keys.push(key1);
            }
        }
        else{
            keys.push(key);
        }
    
    }
    console.log(keys);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2022-07-12
      • 2020-11-07
      • 2017-12-09
      相关资源
      最近更新 更多