简体   繁体   English

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

[英]com.fasterxml.jackson.databind.JsonMappingException while converting Json to Object

I am receiving the below json from an API : 我正在从API接收以下json:

{
  "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.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)

It's an invalid JSON due to multiple occurence of 'responsibility' attribute which leads to duplication of keys. 由于多次出现“ responsibility”属性,这会导致密钥重复,因此它是无效的JSON。 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/# 要检查您的JSON是否正确,请使用任何在线工具,例如http://jsonlint.com/#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决com.fasterxml.jackson.databind.JsonMappingException? - How to solve the com.fasterxml.jackson.databind.JsonMappingException? 无法读取文件:N / A com.fasterxml.jackson.databind.JsonMappingException - Could not read document: N/A com.fasterxml.jackson.databind.JsonMappingException Weblogic 12.2.1.3上的com.fasterxml.jackson.databind.JsonMappingException - com.fasterxml.jackson.databind.JsonMappingException on Weblogic 12.2.1.3 错误:com.fasterxml.jackson.databind.JsonMappingException:未找到序列化程序 - error: com.fasterxml.jackson.databind.JsonMappingException: No serializer found com.fasterxml.jackson.databind.JsonMappingException:无限递归 - com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion com.fasterxml.jackson.databind.JsonMappingException 无法构造类的实例 - com.fasterxml.jackson.databind.JsonMappingException not able to construct instance of class Jboss 7.1 中的 com.fasterxml.jackson.databind.JsonMappingException - com.fasterxml.jackson.databind.JsonMappingException In Jboss 7.1 com.fasterxml.jackson.databind.JsonMappingException由于新的Maven条目 - com.fasterxml.jackson.databind.JsonMappingException due to new maven entries 无法写入 JSON:Infinite recursion(StackOverflowError)nested exception is com.fasterxml.jackson.databind.JsonMappingException:Infinite recursion - Could not write JSON:Infinite recursion(StackOverflowError)nested exception is com.fasterxml.jackson.databind.JsonMappingException:Infinite recursion 无法写入 JSON: (是 java.lang.NullPointerException); 嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException - Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM