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

yrdbyhpb  于 2023-04-20  发布在  其他
关注(0)|答案(2)|浏览(134)

我有这个json:

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

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

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

字段Field1被解析为我想要的String。但是我想让这个方法足够通用,以便在其他地方使用,所以我在helper类中编写了这个方法:

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被解析为Integer。
我可以在helper类中执行以下操作:

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被解析为String),但我真的想把所有Jackson的东西都封装在helper类中,比如TypeReference。这就是说,我不想让调用者使用TypeReference。你有什么想法吗?
先谢谢你了。

ncecgwcz

ncecgwcz1#

阅读文章: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>> {}
}
qkf9rpyu

qkf9rpyu2#

如果你想要不使用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"

相关问题