0

I am trying to convert following JSON to Java object and ending up with UnrecognizedPropertyException.

    {
    "5214": [{
        "name": "sdsds",
        "age": "25",
        "address": null
    },
    {
        "name": "sdfds",
        "age": "26",
        "address": null
    }]
   }

Here "5214" is the random key that I get. I can covert it by modifying JSON little bit. But I want to know whether any possible way to convert the mentioned JSON. I even tried with following snippet taking some reference.

    public class SampleTest {

       private Map<String, List<EmployeeDetails>> employeeDetails = new HashMap<String, List<EmployeeDetails>>();

       public Map<String, List<EmployeeDetails>> getEmployeeDetails() {
              return employeeDetails;
       }

       public void setEmployeeDetails(Map<String, List<EmployeeDetails>> employeeDetails) {
              this.employeeDetails = employeeDetails;
       }

   }


   public class EmployeeDetails {

       private String name;
       private String age;
       private String address;

       //Getters and Setters
   }

Can someone guide me on this?

User 0234
  • 135
  • 2
  • 11
  • Does this answer your question? [How to use Jackson to deserialise an array of objects](https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects) – Navin Gelot Dec 24 '19 at 07:25
  • @NavinGelot That question is to create a list of objects to deserialise into JSON – User 0234 Dec 24 '19 at 09:30
  • Yes, I know :), but the answer to that post is your solution – Navin Gelot Dec 24 '19 at 09:59

3 Answers3

1

Use Type Reference (Import Jackson Package for Java)

TypeReference<Map<String, List<EmployeeDetails>>> typeReference = new TypeReference<Map<String, List<EmployeeDetails>>>()
{
};                                                    
Map<String, List<EmployeeDetails>> employeeDetails = new ObjectMapper().readValue(jsonString, typeReference);
Sugan V
  • 89
  • 2
  • 6
0

Check something from that

Maybe:

public class Data {

    // String contain the Key, for example: 5214
    Map<String, List<EmployeeDetails>> employeeDetails = 
        new HashMap<String,List<EmployeeDetails>>();

    public Data() {

    }

    @JsonAnyGetter
    public Map<String, List<EmployeeDetails>> getEmployeeDetails() {
        return employeeDetails;
    }
}
lczapski
  • 4,026
  • 3
  • 16
  • 32
0

I would use custom deserializer with few helper classes. To make the code (matter of opinion I guess) clearer, create the list object:

@SuppressWarnings("serial")
@Getter @Setter
public class EmployeeDetailsList extends ArrayList<EmployeeDetails> {
    // this will hold the arbitrary name of list. like 5214
    private String name;
}

Then this list seems to be inside an object, say Wrapper:

@Getter
@RequiredArgsConstructor
@JsonDeserialize(using = WrapperDeserializer.class)
public class Wrapper {
    private final EmployeeDetailsList employeeDetailsList;
}

So there is annotation @JsonDeserializer that handles deserializing Wrapper. It is not possible to directly deserialize unknown field names to some defined type so we need to use mechanism like this custom deserializer that inspects what is inside Wrapper and determines what to deserialize and how.

And here is how the deserializer works:

public class WrapperDeserializer extends JsonDeserializer<Wrapper> {

    private final ObjectMapper om = new ObjectMapper();
    @Override
    public Wrapper deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        TreeNode node = p.readValueAsTree();
        // This is the place for caution. You should somehow know what is the correct node
        // Here I happily assume there is just the one and first
        String fName = node.fieldNames().next();

        EmployeeDetailsList edl = om.readValue(node.get(fName).toString(),
               EmployeeDetailsList.class); 
        edl.setName(fName);
        return new Wrapper(edl);
    }

}

Please check it carefully it is not perfect in sense finding alwasy the correct node and maybe the instantiation can be done in other ways better. But it shoudl give you a hunch how it could be done.

pirho
  • 11,565
  • 12
  • 43
  • 70