简体   繁体   中英

Extract parameters from nested Json

I have an json string, which looks like this:

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

I want to deserialized entityattribute to an object:

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

One way is to extract entityId and attributeType first using the json path(ie entityattribute/entityId)and create an object EntityAttributeNotification.

I want to know if there is a way to directly deserialized entityattribute to EntityAttributeNotification. I have also tried with JsonMixin annotation but this does not apply here.

Through the following method you can extract Parameters and Values of nested JSON .

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);

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