我无法让 Jackson 对象映射器正确设置类型

问题描述 投票:0回答:2

我有这个 json:

{
 "List": [
  {
   "Field1": 1678493915,
   "Field2": "A string"
  }
 ]
}

当我在辅助类中定义这个方法时:

  public static Map<String, List<Map<String, String>>> readJSON(String json) throws JsonProcessingException {
      return new ObjectMapper().readValue(json, new TypeReference<>() {});
  }

字段

Field1
被解析为我想要的字符串。但我想让这个方法足够通用,以便在其他地方使用,所以我在帮助类中写了这个:

  public static <T> T readJSON2(String json) throws JsonProcessingException {
      return new ObjectMapper().readValue(json, new TypeReference<>(){});
  }

我打电话给:

Map<String, List<Map<String, String>>> data = readJSON2(jsonString);

不幸的是,在这种情况下,

Field1
被解析为整数。

我可以在助手类中做以下事情:

  public static <T> T readJSON3(String json, TypeReference<T> typeReference) throws JsonProcessingException {
      return new ObjectMapper().readValue(json, typeReference);
  }

这样称呼它:

Map<String, List<Map<String, String>>> data = readJSON3(jsonString, new TypeReference<>(){});

这行得通(

Field1
被解析为字符串),但我真的很想将所有 Jackson 的东西封装在辅助类中,例如 TypeReference。也就是说,我不希望调用者使用 TypeReference。有什么想法吗?

提前致谢。

java jackson objectmapper json-serialization
2个回答
0
投票

阅读文章:https://www.baeldung.com/jackson-linkedhashmap-cannot-be-cast(第 6 段)。由于运行时的性质,使用泛型类型是有限制的。

如我所见,您可以使用以下解决方案:

public class Program {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {
        String json = "{\n" +
                " \"List\": [\n" +
                "  {\n" +
                "   \"Field1\": 1678493915,\n" +
                "   \"Field2\": \"A string\"\n" +
                "  }\n" +
                " ]\n" +
                "}";
        
        Map<String, List<CustomType>> res = func(json, CollectionCustomType.class);

    }

    private static <T> T func(String json, Class<T> type) throws IOException {
        JavaType javaType = mapper.getTypeFactory().constructType(type);
        return mapper.readValue(json, javaType);

    }


    @Data
    private static class CustomType {

        @JsonProperty("Field1")
        private String field1;

        @JsonProperty("Field2")
        private String field2;
    }
    
    private static class CollectionCustomType extends HashMap<String, List<CustomType>> {}
}


0
投票

如果你想要不使用 TypeReference 的简单方法,只需考虑使用

mapper.readTree(json)
.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;

public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    String json = "{\"List\":[{\"Field1\":1678493915,\"Field2\":\"A string\"}]}";
    JsonNode data = mapper.readTree(json);

    System.out.println(mapper.writeValueAsString(data));
    System.out.println(data.get("List"));
    System.out.println(data.get("List").get(0));
    System.out.println(data.get("List").get(0).get("Field1"));
    System.out.println(data.get("List").get(0).get("Field2"));
}

输出将是:

{
  "List" : [ {
    "Field1" : 1678493915,
    "Field2" : "A string"
  } ]
}
[{"Field1":1678493915,"Field2":"A string"}]
{"Field1":1678493915,"Field2":"A string"}
1678493915
"A string"
© www.soinside.com 2019 - 2024. All rights reserved.