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?

3
  • Does this answer your question? How to use Jackson to deserialise an array of objects Commented Dec 24, 2019 at 7:25
  • @NavinGelot That question is to create a list of objects to deserialise into JSON Commented Dec 24, 2019 at 9:30
  • Yes, I know :), but the answer to that post is your solution Commented Dec 24, 2019 at 9:59

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I think there is a typo: TypeReferenceMap<String, List<EmployeeDetails>> is supposed to be TypeReference<Map<String, List<EmployeeDetails>>>.
Yeah. You are Right. Thanks for Correcting
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;
    }
}

2 Comments

Nope. I tried with constructor as well and got com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "5214"
Do you try with JsonAnyGetter @User0234
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.