猴王牌 2013-04-26 16:55 采纳率: 50%
浏览 356
已采纳

如何使用jackson反序列化json到对象

[code="java"]
{"indexs":[{"id":"6310_2","createTime":1359424596001,"indexFields":[{"name":"keyword_score","type":1,"analyzed":true,"highlight":true,"strValue":"2.1781344","longValue":0,"intValue":0},{"name":"object_id","type":1,"analyzed":true,"highlight":true,"strValue":"6310","longValue":0,"intValue":0},{"name":"object_type","type":1,"analyzed":true,"highlight":true,"strValue":"2","longValue":0,"intValue":0},{"name":"user_id","type":1,"analyzed":true,"highlight":true,"strValue":"657","longValue":0,"intValue":0},{"name":"user_name","type":1,"analyzed":true,"highlight":true,"strValue":"好书推荐","longValue":0,"intValue":0},{"name":"publish_time","type":1,"analyzed":true,"highlight":true,"strValue":"1324892396000","longValue":0,"intValue":0},{"name":"name","type":1,"analyzed":true,"highlight":true,"strValue":"水浒传","longValue":0,"intValue":0}]}]}

[/code]

当前Indexs中只有一个Index对象
多格式 如何反序列化成Index的集合

求大家指点

对象类型格式如下
[img]http://dl.iteye.com/upload/picture/pic/125114/446fc4dc-8a25-347b-9446-128a555e9aa5-thumb.png[/img]

  • 写回答

3条回答

  • Dead_Knight 2013-04-26 20:45
    关注

    写个完整的,供你参考:
    [code="java"]
    import java.util.List;

    import org.codehaus.jackson.JsonNode;
    import org.codehaus.jackson.map.ObjectMapper;

    public class TestJackson {
    private static String json = "{\"indexs\":[{\"id\":\"6310_2\",\"createTime\":1359424596001,\"indexFields\":[{\"name\":\"keyword_score\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"2.1781344\",\"longValue\":0,\"intValue\":0},{\"name\":\"object_id\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"6310\",\"longValue\":0,\"intValue\":0},{\"name\":\"object_type\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"2\",\"longValue\":0,\"intValue\":0},{\"name\":\"user_id\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"657\",\"longValue\":0,\"intValue\":0},{\"name\":\"user_name\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"好书推荐\",\"longValue\":0,\"intValue\":0},{\"name\":\"publish_time\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"1324892396000\",\"longValue\":0,\"intValue\":0},{\"name\":\"name\",\"type\":1,\"analyzed\":true,\"highlight\":true,\"strValue\":\"水浒传\",\"longValue\":0,\"intValue\":0}]}]}";

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper(); 
        JsonNode jsonNode = mapper.readTree(json); 
        JsonNode jn = jsonNode.get("indexs");
        System.out.println(jn.toString());
    
        Index p = mapper.readValue(json, Index.class); 
        System.out.println(p.getIndexs());
        List<Model> indexs = p.getIndexs();
        for(Model m : indexs) {
            System.out.println(m.getId());
        }
    }
    

    }

    class Index {
    private List indexs;

    public List<Model> getIndexs() {
        return indexs;
    }
    
    public void setIndexs(List<Model> indexs) {
        this.indexs = indexs;
    }
    

    }

    class Model {
    private String id;
    private long createTime;
    private List indexFields;
    public Model() {

    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public long getCreateTime() {
        return createTime;
    }
    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }
    public List<Field> getIndexFields() {
        return indexFields;
    }
    public void setIndexFields(List<Field> indexFields) {
        this.indexFields = indexFields;
    }
    

    }

    class Field {
    private String name;
    private int type;
    private boolean analyzed;
    private boolean highlight;
    private String strValue;
    private long longValue;
    private int intValue;

    public Field() {
    
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    public boolean isAnalyzed() {
        return analyzed;
    }
    public void setAnalyzed(boolean analyzed) {
        this.analyzed = analyzed;
    }
    public boolean isHighlight() {
        return highlight;
    }
    public void setHighlight(boolean highlight) {
        this.highlight = highlight;
    }
    public String getStrValue() {
        return strValue;
    }
    public void setStrValue(String strValue) {
        this.strValue = strValue;
    }
    public long getLongValue() {
        return longValue;
    }
    public void setLongValue(long longValue) {
        this.longValue = longValue;
    }
    public int getIntValue() {
        return intValue;
    }
    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿