dove4evr
dove4evr

Reputation: 83

com.fasterxml.jackson.databind.JsonMappingException while converting Json to Object

I am receiving the below json from an API :

{
  "list": {

    "responsibility": 
    {
    "rolename" : "Regional Operation Director",
    "organizationLevelValues":"32R",
    "otherCMDLevelValues":"MGT,"
    },
    "responsibility": 
    {
    "rolename" : "Legal Representative",
    "organizationLevelValues":"VALEO",
    "otherCMDLevelValues":"MGT,RO04,"
    }
  }
}

i am trying to convert this into object:

public class ActorResponsibilityResp {
    private ActorResponsibilities list;

    public ActorResponsibilities getList() {
        return list;
    }

    public void setList(ActorResponsibilities list) {
       this.list = list;
    }
}

public class ActorResponsibilities {
   private List<ActorResponsibility> responsibility;

   public List<ActorResponsibility> getResponsibility() {
       return responsibility;
   }

   public void setResponsibility(List<ActorResponsibility> responsibility) {
        this.responsibility = responsibility;
    }
}

public class ActorResponsibility {
    @JsonProperty("rolename")
    private String rolename;
    @JsonProperty("organizationLevelValues")
    private String organizationLevelValues;
    @JsonProperty("otherCMDLevelValues")
    private String otherCMDLevelValues;

    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String getOrganizationLevelValues() {
        return organizationLevelValues;
    }

    public void setOrganizationLevelValues(String organizationLevelValues) {
        this.organizationLevelValues = organizationLevelValues;
    }

    public String getOtherCMDLevelValues() {
        return otherCMDLevelValues;
    }

    public void setOtherCMDLevelValues(String otherCMDLevelValues) {
        this.otherCMDLevelValues = otherCMDLevelValues;
    }
}

I am getting the below exception when i use com.fasterxml.jackson.databind.ObjectMapper.readvalue(reponseStr, ActorResponsibilityResp.class):

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize     instance of java.util.ArrayList out of START_OBJECT token at [Source:
{
  "list": {

      "responsibility": 
      {
        "rolename" : "Regional Operation Director",
        "organizationLevelValues":"32R",
        "otherCMDLevelValues":"MGT,"
      },
     "responsibility": 
      {
         "rolename" : "Legal Representative",
         "organizationLevelValues":"VALEO",
         "otherCMDLevelValues":"MGT,RO04,"
       }
    }
 }; line: 4, column: 5] (through reference chain: com.valeo.vs.model.ActorResponsibilityResp["list"]->com.valeo.vs.model.ActorResponsibilities["responsibility"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:256)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:214)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2098)

Upvotes: 0

Views: 2851

Answers (1)

Manish Singh
Manish Singh

Reputation: 538

It's an invalid JSON due to multiple occurence of 'responsibility' attribute which leads to duplication of keys. Your code seems perfect.

    {
      "list": {

        "responsibility": [
        {
        "rolename" : "Regional Operation Director",
        "organizationLevelValues":"32R",
        "otherCMDLevelValues":"MGT,"
        }, 
        {
        "rolename" : "Legal Representative",
        "organizationLevelValues":"VALEO",
        "otherCMDLevelValues":"MGT,RO04,"
        }
      ]
    }
}

To Check whether your JSON is correct, use any online tool like http://jsonlint.com/#

Upvotes: 1

Related Questions