当随机生成属性密钥时,如何使用Jackson将JSON从POJO序列化和反序列化到POJO

iyr7buue  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(187)

我正在从一个服务接收JSON,其中包含随机生成的属性密钥

{
    "random1" : {
        "name" : "john",
        "lastName" : "johnson"
    },
    "nextRandom500" : {
        "name" : "jack",
        "lastName" : "jackson"
    },
    "random100500" : {
        "name" : "jack",
        "lastName" : "johnson"
    }   
}

我已经使用Jackson.annotations-2.13.4和java 11创建了POJO类

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder(toBuilder=true)
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponse {
private Map<String,User> users;

    @Data
    @SuperBuilder(toBuilder=true)
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class User {
        private String name;
        private String lastName;
    }

}

但当我试图反序列化它,我收到错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "random1" (class com.pingidentity.UserResponse), not marked as ignorable (one known property: "variables"\])
at \[Source: (String)"{
"random1":
{
"name": "john",
"lastName": "johnson"
},
"nextRandom500":
{
"name": "jack",
"lastName": "jackson"
},
"random100500":
{
"name": "john",
"lastName": "jackson"
}
}"; line: 3, column: 6\] (through reference chain: com.pingidentity.UserResponse\["random1"\])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:1127)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:2023)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1700)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1678)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:320)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.jackson.databind.ObjectMapper.\_readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3597)
at com.pingidentity.Test.main(Test.java:507)


因此,我正在寻找一种方法,如何正确地反序列化这样一个JSON和如何序列化它,如果我会使用这个POJO创建另一个服务相同的JSON将感谢您的帮助!

pu3pd22g

pu3pd22g1#

我假设您尝试将此JSON反序列化为UserResponse
在这种情况下,JSON将需要具有属性Map的属性users

{
  "users": {
    "random1" : {
      "name" : "john",
      "lastName" : "johnson"
    },
    "nextRandom500" : {
      "name" : "jack",
      "lastName" : "jackson"
    },
    "random100500" : {
      "name" : "jack",
      "lastName" : "johnson"
    }
  }
}

您也可以使用以下命令读取原始JSON:

Map<String,User> users = objectMapper.readValue(json, 
    new TypeReference<Map<String,User>>(){});

相关问题