首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObjectMapper readValue

ObjectMapper readValue
EN

Stack Overflow用户
提问于 2017-03-09 17:33:25
回答 3查看 32.3K关注 0票数 3

我加载了文本格式的ressource文件json

代码语言:javascript
复制
{
    "sources": [{
            "prop1": "1",
            "prop2": "2"

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

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

        }
    ]
}

我有一个具有prop1和prop2属性的类

我想用ObjectMapper恢复一个list类。方法是什么?

此代码不起作用...

代码语言:javascript
复制
 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>>(){});

谢谢你的帮忙

EN

回答 3

Stack Overflow用户

发布于 2017-03-09 17:49:51

在您的例子中,我会编写一个自定义的JsonDeserializer。我还没有真正测试过代码,但我认为这个想法很清楚:

代码语言:javascript
复制
    final MyClassDeserializer myClassDeserializer = new MyClassDeserializer();
    final SimpleModule deserializerModule = new SimpleModule();
    deserializerModule.addDeserializer(MyClass.class, myClassDeserializer);

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

JsonDeserializer的代码

代码语言:javascript
复制
    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());
            ...
        }

        ...
    }
票数 2
EN

Stack Overflow用户

发布于 2017-03-09 17:56:42

首先:您的JSON是无效的。在sources数组中的第二个对象之后有一个逗号。这必须被删除。

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

有了这些考虑,您可以使用ObjectMappergetTypeFactory()方法构造一个MapType,并使用构造的类型反序列化该值,如下所示。

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2017-03-09 17:57:31

我实际上投票给了另一个答案,但这是我的想法,创建类并让jackson来做工作:

代码语言:javascript
复制
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;
    }

}

输出为:

代码语言:javascript
复制
{"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'}]}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42691591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档