java什么是根节点_java – 为什么我不能打开根节点并反序列...

为什么我无法通过展开根节点来反序列化对象数组?

import java.io.IOException;

import java.util.Arrays;

import java.util.List;

import org.codehaus.jackson.map.DeserializationConfig;

import org.codehaus.jackson.map.ObjectMapper;

import org.codehaus.jackson.map.annotate.JsonRootName;

import org.junit.Assert;

import org.junit.Test;

public class RootNodeTest extends Assert {

@JsonRootName("customers")

public static class Customer {

public String email;

}

@Test

public void testUnwrapping() throws IOException {

String json = "{\"customers\":[{\"email\":\"hello@world.com\"},{\"email\":\"john.doe@example.com\"}]}";

ObjectMapper mapper = new ObjectMapper();

mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

List customers = Arrays.asList(mapper.readValue(json, Customer[].class));

System.out.println(customers);

}

}

我一直在挖掘杰克逊的文档,这是我能想到的,但在运行它时,我收到以下错误:

A org.codehaus.jackson.map.JsonMappingException has been caught, Root name 'customers' does not match expected ('Customer[]') for type [array type, component type: [simple type, class tests.RootNodeTest$Customer]] at [Source: java.io.StringReader@49921538; line: 1, column: 2]

我想在不创建包装类的情况下完成此任务.虽然这是一个示例,但我不想仅为展开根节点创建不必要的包装类.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值