将Json转换为Object时的com.fasterxml.jackson.databind.JsonMappingException

时间:2016-10-14 10:33:25

标签: java json rest jackson objectmapper

我从API接收以下json:

{
  "list": {

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

我正在尝试将其转换为对象:

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

当我使用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)

1 个答案:

答案 0 :(得分:1)

由于“责任”属性的多次出现导致密钥重复,因此它是无效的JSON。你的代码看起来很完美。

    {
      "list": {

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

要检查您的JSON是否正确,请使用任何在线工具,例如http://jsonlint.com/#