如何使用Jackson解析JSON数组响应?

Vla*_*lad 0 arrays parsing android json jackson

我正在为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
Run Code Online (Sandbox Code Playgroud)

每个都是一个类似汽车的汽车:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我一直试图用杰克逊自动解析它很少/没有成功..是否有可能自动解析并将其转换为对象?我无法在网上任何地方找到这个..
如果你有这种特定类型的服务器响应的某种例子请指出我,或者如果有人一般可以解释如何使用杰克逊或其他工具,我会大大欣赏它.


编辑:谢谢大家.我设法通过删除{"cars":结果字符串的开头和结果字符串}的结尾来使杰克逊工作.在这之后,杰克逊明白它是一个阵列并且自己做了一切.所以对于那些遇到这类问题的人来说:JSON数组应该从头开始[并结束,]内部的每个元素都应该从头开始{并结束}.不需要注释,杰克逊可以自己找到成员.

Sta*_*Man 7

杰克逊当然可以处理这个.但是你需要更多的东西.首先,绑定到的请求对象:

public class Response {
  public List<Cars> cars;
}
Run Code Online (Sandbox Code Playgroud)

你还需要向汽车添加setter,公开字段(Jackson只考虑公共字段进行自动检测),或者将以下注释添加到Cars类:

@JsonAutoDetect(fieldVisibility=Visibility.ANY)
Run Code Online (Sandbox Code Playgroud)

(这样私有字段也被视为属性).

有了它,你做:

Response response = new ObjectMapper().readValue(jsonInput, Response.class);
Run Code Online (Sandbox Code Playgroud)