简体   繁体   中英

Jackson fasterxml pojo to map conversion with datatype

I wanted to create a cxf request object by mapping dynamic attributes to a hashmap with a key as value of the key and value as value by converting to a specific datatype which is mentioned in the type.

My request looks like

{
  "id":"10001",
  "name":"suresh",
  "dynamicAttribute":[
    { 
      "key":"dob",
      "value":"02/05/2016",
      "type":"date"
    },
    {
      "key":"age",
      "value":"27",
      "type":"int"
    }
  ]
}

Please let me know the possible ways to convert this...

First create request pojo class mapped with the json request, in which you will have array list for "dynamicAttribute". Once you get the request converted to pojo, iterate over the "dynamicAttribute" array list, inside the loop use switch statement of "type", mention all the cases for types (example - date, int, double), create object inside all cases based on the type, and put it in a Map where key will be the key string (example - dob, age) and value will be the type object. This may not be the best solution, but you can give it a try.

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