String text = ...; // [{/* header */}, {/* body */}]Type[] types = new Type[] {Header.class, Body.class};List<Object> list = JSON.parseArray(text, types);Header header = (Header) list.get(0);Body body = (Body) list.get(1);
使用@JSONCreator来指定构造函数来创建对象如果你的JavaBean没有缺省构造函数,可以使用@JSONCreator来指定构造函数
public static class Entity {private final int id;private final String name;@JSONCreatorpublic Entity(@JSONField(name = "id") int id, @JSONField(name = "name") String name){this.id = id;this.name = name;}public int getId() { return id; }public String getName() { return name; }}
把JSON文本反序列化为一个原型接口public static interface Bean {int getId();void setId(int value);String getName();void setName(String value);}String text = "{/"id/":123, /"name/":/"chris/"}";Bean bean = JSON.parseObject(text, Bean.class);// 按接口调用Assert.assertEquals(123, bean.getId());Assert.assertEquals("chris", bean.getName());bean.setId(234);Assert.assertEquals(234, bean.getId());出自:?pageId=2424946