ObjectMapper readValue

时间:2017-03-09 09:33:25

标签: java spring objectmapper

我加载了一个ressource文件json 使用文本格式

{
    "sources": [{
            "prop1": "1",
            "prop2": "2"

        },
        {
            "prop1": "1",
            "prop2": "2"

        },
    ],
    "redirection": [{
            "prop1": "1",
            "prop2": "2"

        }
    ]
}

我有一个具有此属性prop1和prop2

的类

我想用ObjectMapper恢复列表类。方法是什么?

此代码不起作用....

 Map<String, Object> mp =  mapper.readValue(jsonResource.getInputStream(),new TypeReference<Map<String, Object>>() {});
String sourceText= new ObjectMapper().readTree(jsonResource.getInputStream()).get("sources").asText();

 mapper.readValue(sourceText, new TypeReference<List<MyClass>>(){});

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

在您的情况下,我会写一个自定义JsonDeserializer。 Haven没有真正测试过代码,但我认为这个想法很明确:

    final MyClassDeserializer myClassDeserializer = new MyClassDeserializer();
    final SimpleModule deserializerModule = new SimpleModule();
    deserializerModule.addDeserializer(MyClass.class, myClassDeserializer);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(deserializerModule);

JsonDeserializer的代码:

    public class MyClassDeserializer extends JsonDeserializer<MyClass> {

    @Override
    public MyClass deserialize(final JsonParser jsonParser, final DeserializationContext context)
            throws IOException {
        final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        final JsonNode sourcesNode = node.get("sources");
        if(node.isArray()) {
            final ArrayNode arrayNode = (ArrayNode) node;
            final Iterable<JsonNode> nodes = arrayNode::elements;
            final Set<Source> set = StreamSupport.stream(nodes.spliterator(), false)
                    .map(mapper)
                    .collect(Collectors.toSet());
            ...
        }

        ...
    }

答案 1 :(得分:0)

第一件事:您的JSON无效。 sources数组中的第二个对象后面有一个逗号。这必须删除。

第二:我认为你没有为你的结果选择合适的类型。您的JSON表示的是一个从字符串映射到对象数组的映射。所以类型应该类似于Map<String, Props[]>(因为你没有提供你班级的名字,我称之为Props

通过这些注意事项,您可以使用MapType s ObjectMapper方法构建getTypeFactory(),并使用构造的类型反序列化值,如下所示。

ObjectMapper mapper = new ObjectMapper();
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Props[].class);
Map<String, Props[]> map = mapper.readValue(s, mapType);

答案 2 :(得分:0)

我实际上投了另一个答案,但这是我的想法,创建课程并让杰克逊完成工作:

public class ResourceTest {

    @Test
    public void test1() throws IOException {
        assertTrue(true);

        Resource resource = new Resource();

        resource.getRedirectrions().add(makeRedirectrion("rprop11", "rprop12"));
        resource.getRedirectrions().add(makeRedirectrion("rprop21", "rprop22"));

        resource.getSources().add(makeSource("sprop11","sprop12"));
        resource.getSources().add(makeSource("sprop21","sprop22"));

        String json = new ObjectMapper().writeValueAsString(resource);
        System.out.println(json);

        Resource resource1 = new ObjectMapper().readValue(json, Resource.class);
        System.out.println(resource1);
    }

    private Source makeSource(String prop1, String prop2) {
        Source source = new Source();
        source.setProp1(prop1);
        source.setProp2(prop2);
        return source;
    }

    private Redirectrion makeRedirectrion(String prop1, String prop2) {
        Redirectrion redirectrion = new Redirectrion();
        redirectrion.setProp1(prop1);
        redirectrion.setProp2(prop2);
        return redirectrion;
    }

}

输出是:

{"sources":[{"prop1":"sprop11","prop2":"sprop12"},{"prop1":"sprop21","prop2":"sprop22"}],"redirectrions":[{"prop1":"rprop11","prop2":"rprop12"},{"prop1":"rprop21","prop2":"rprop22"}]}
Resource{sources=[Source{prop1='sprop11', prop2='sprop12'}, Source{prop1='sprop21', prop2='sprop22'}], redirectrions=[Source{prop1='rprop11', prop2='rprop12'}, Source{prop1='rprop21', prop2='rprop22'}]}