如何使用杰克逊解析JSON阵列响应? [英] How to parse JSON array response using Jackson?

查看:118
本文介绍了如何使用杰克逊解析JSON阵列响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个RESTful客户端Android和我有一个关于杰克逊的问题。
我得到以下JSON响应:

I'm building a RESTful client for Android and I have a question about Jackson.
I get the following JSON response:

{
    "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

每一个汽车从一个类,看起来像这样:

each is a car from a class that looks like this:

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;
    }
}

我一直在试图与杰克逊自动解析它很少/没有成功.. 它甚至有可能来分析,它会自动转换为对象? 我无法找到这个网上的任何地方..
如果你有某种例子服务器响应的这种特定类型的请点我在那里,或者如果有人能大致解释如何与杰克逊或与其他一些工具,我将不胜AP preciate它做到这一点。

I've been trying to parse it automatically with Jackson with little/no success.. Is it even possible to parse and convert it to an object automatically? I can't find this anywhere online..
If you have some sort of example for this specific type of server response please point me there, or if someone can generally explain how to do it with Jackson or with some other tool, I would greatly appreciate it.

编辑: 谢谢大家。我设法通过删除 {车:从结果字符串的开头,而}使杰克逊工作从结果字符串的结尾。这样做之后,杰克逊知道这是一个数组,并自己所做的一切。因此,对于任何人谁得到的问题与那些样的东西:一个JSON数组应该从 [结束] 每个元素中应启动{结束} 。没有说明需要,杰克逊可以自己找到的成员。

Thanks all. I managed to make Jackson work by removing the {"cars": from the beginning of the result string, and the } from the end of the result string. After doing that, Jackson understood it was an array and did everything by itself. So for anyone else who got problems with those sort of things: A JSON array should start with [ and end with ] and each element inside should start with { and end with }. No annotations needed, Jackson can find the members by itself.

推荐答案

杰克逊肯定能处理这个问题。然而,你需要的夫妇多件。 首先,请求对象绑定到:

Jackson certainly can handle this. You need couple more pieces however. First, the request object to bind to:

public class Response {
  public List<Cars> cars;
}

和您还需要以添加setters来汽车,使公共领域(杰克逊只考虑公共字段用于自动检测),或添加下面的注释,以汽车类:

and you also need to either add setters to Cars, make fields public (Jackson only considers public fields for auto-detection), or add following annotation to Cars class:

@JsonAutoDetect(fieldVisibility=Visibility.ANY)

(使私人领域被认为是性能以及)。

(so that private fields are considered properties as well).

有了这样的,你做的:

Response response = new ObjectMapper().readValue(jsonInput, Response.class);

这篇关于如何使用杰克逊解析JSON阵列响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆