猴王牌 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条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功