java - 将 Json 转换为对象时出现 com.fasterxml.jackson.databind.JsonMappingException

标签 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)

最佳答案

由于多次出现“responsibility”属性,导致 key 重复,因此这是一个无效的 JSON。您的代码看起来很完美。

    {
      "list": {

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

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

关于java - 将 Json 转换为对象时出现 com.fasterxml.jackson.databind.JsonMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041047/

相关文章:

java - 阻止通过spring boot应用程序中的url栏转到其他网页

javascript - 根据多个过滤器从 json 数据计算平均值

java - 如何将 Java 接口(interface)迁移到微服务?

java - jboss Resteasy参数注入(inject)@Context

java - Jersey 给出运行时错误 java.lang.NoClassDefFoundError : org/glassfish/jersey/server/ResourceConfig

Java:文件到字符串,使用缓冲区的问题,字节数组不干净

java - 如何在 Gson 中反序列化具有两种可能类型( boolean 值和字符串)的字段

java - 有什么方法可以调用 aerospike 中的某些脚本或警报

javascript - 将 json 对象从 php 传递到 javascript

java - 使用 JAVA 将图像上传到 ActiveCollab API 时出现问题