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;
}
}
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.
EDIT:
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:
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:
(so that private fields are considered properties as well).
And with that, you do:
I did something similar using the Gson library (http://code.google.com/p/google-gson/). I found it easier to use than Jackson.
Here's an example of parsing a response for a contact that looks like it would translate well to your Cars example:
I don't have any knowledge of Jackson I'm afraid, but a possibility could be to have a constructor in the
Cars
class that takes as a parameter an element of a JSON array (whatever type that is in Jackson). The constructor would then pull out the values from each key of the JSON car entry and populate the class members as appropriate.Here's a simple test I used at some point to understand how Jackson serializes/deserializes JSON arrays and objects: