如何使用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个回答

写个完整的,供你参考:
[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]

dbh0512
猴王牌 谢谢你
接近 7 年之前 回复
Dead_Knight
Dead_Knight 不是这个原因,跟包结构没关系的。
接近 7 年之前 回复
dbh0512
猴王牌 我的类都是写在其他包里的类 不是直接写在下面的类 不知道是不是这个原因 我把代码复制到一个空项目中 就可以完美运行
接近 7 年之前 回复
Dead_Knight
Dead_Knight 可能是你的bean写的有问题啊,没有与json相匹配的。类名称换掉没关系,里面的属性一定要匹配上
接近 7 年之前 回复
dbh0512
猴王牌 我按照你的方法 我的Indexs就是你写的Index , Index是你写的Model 在Index p = mapper.readValue(json, Index.class); 时出错 我的是(Indexs p = mapper.readValue(json, Indexs.class);); No suitable constructor found for type [simple type, class com.abc.search.vo.IndexField]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@26514577; line: 1, column: 70] (through reference chain: com.abc.search.pojo.Indexs["indexs"]->com.abc.search.vo.Index["indexFields"])
接近 7 年之前 回复

[code="java"]
String personstr = getRequest().getParameter("persons");
JSONArray json = JSONArray.fromObject(personstr);
List persons = (List)JSONArray.toCollection(json, nvoidPerson.class);

[/code],[url]http://blog.sina.com.cn/s/blog_5920510a01011vu8.html[/url]

给你写个简单的例子,仅供参考:
[code="java"]
import java.util.List;

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

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");
    String id = jsonNode.get("indexs").toString(); 
    System.out.println(id);
    //List list =  mapper.readValue(person, new TypeReference<List>() {
    //}); 
    //Person p = mapper.readValue(person,Person.class); 
}

}
[/code]

jackson主要使用下面两段代码完成反序列化话:
[code="java"]
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json); //json为json字符串
[/code]
然后通过jsonNode再做相应的处理。如对于对象转换,直接使用
Person p = mapper.readValue(person,Person.class);

如果对于List类型,使用:
[code="java"]
List list = mapper.readValue(person, new TypeReference() {
});
[/code]

dbh0512
猴王牌 不是很明白 jsonNode.get("indexs").toString();返回的还是Json数据字符串 mapper.readValue(person,Person.class); 转成对象 我需求里这个对象中有一个集合 集合都是Object类型的 会报一个错 org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.motie.search.vo.Index out of START_ARRAY token at [Source: java.io.StringReader@7ff4d7c0; line: 1, column: 1]
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问