我正在为 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 或其他一些工具来完成此操作,我将不胜感激。
编辑:
谢谢大家。我设法让杰克逊工作通过删除{"cars":
从结果字符串的开头开始,并且}
从结果字符串的末尾开始。完成之后,杰克逊明白这是一个数组,并自行完成了所有操作。因此,对于遇到此类问题的其他人:JSON 数组应该以[
并结束于]
里面的每个元素应该以{
并结束于}
。不需要注释,Jackson 可以自己找到成员。