【发布时间】:2011-08-04 12:01:24
【问题描述】:
我正在为 Android 构建一个 RESTful 客户端,我有一个关于 Jackson 的问题。
我收到以下 JSON 响应:
{
"cars": [
{
"active": "true",
"carName": "××× ×'פ ס××××§×",
"categoryId": {
"licenseType": "××××××",
"licenseTypeId": "1"
},
"id": "1401268",
"insuranceDate": "2011-07-05T00:00:00+03:00",
"lessonLength": "45",
"licenseDate": "2011-07-05T00:00:00+03:00",
"price": "100",
"productionYear": "2009-07-05T00:00:00+03:00"
},
{
"active": "true",
"carName": "××©× ×××",
"categoryId": {
"licenseType": "×ש××ת",
"licenseTypeId": "4"
},
"id": "1589151",
"insuranceDate": "2011-04-13T00:00:00+03:00",
"lessonLength": "30",
"licenseDate": "2011-04-13T00:00:00+03:00",
"price": "120",
"productionYear": "2004-04-12T00:00:00+03:00"
},............. etc
每辆车都是来自类似这样的类别的汽车:
public class Cars implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String carName;
private Date productionYear;
private Date insuranceDate;
private Date licenseDate;
private Boolean active;
private Long price;
private Integer lessonLength;
private Date dayStart;
// private Collection<Students> studentsCollection;
// private Collection<Lessons> lessonsCollection;
private LicenseTypes categoryId;
// private Collection<Kilometers> kilometersCollection;
public Cars() {
}
public Cars(Integer id) {
this.id = id;
}
public Cars(Integer id, String carName) {
this.id = id;
this.carName = carName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
我一直在尝试用 Jackson 自动解析它,但几乎没有成功。
甚至可以自动解析并将其转换为对象吗?
我在网上的任何地方都找不到这个..
如果您有这种特定类型的服务器响应的某种示例,请指出我那里,或者如果有人可以一般地解释如何使用 Jackson 或其他工具进行操作,我将不胜感激。
编辑:
谢谢大家。我设法通过删除结果字符串开头的{"cars": 和结果字符串末尾的} 来使Jackson 工作。做完之后,Jackson 明白它是一个数组,并且自己做了所有的事情。因此,对于遇到此类问题的其他任何人:JSON 数组应以[ 开头并以] 结尾,并且其中的每个元素都应以{ 开头并以} 结尾。无需注释,Jackson 可以自己找到成员。
【问题讨论】:
标签: android json parsing jackson arrays