我正在为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数组应以[
开头,以]
结尾,内部的每个元素都应以{
开头,以{{1结尾}}。不需要注释,杰克逊可以自己找到成员。
答案 0 :(得分:7)
public class Response {
public List<Cars> cars;
}
你还需要向汽车添加设置器,公开字段(杰克逊只考虑公共字段进行自动检测),或者将以下注释添加到汽车类:
@JsonAutoDetect(fieldVisibility=Visibility.ANY)
(因此私有字段也被视为属性)。
然后,你这样做:
Response response = new ObjectMapper().readValue(jsonInput, Response.class);
答案 1 :(得分:4)
这是我在某个时候使用的一个简单测试,用于理解Jackson如何序列化/反序列化JSON数组和对象:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JacksonTest {
private final ObjectMapper mapper = new ObjectMapper();
public static class Book {
public String title;
public String author;
}
@Test
public void readWriteJsonObject() throws JsonGenerationException, JsonMappingException, IOException {
String json = mapper.writeValueAsString(new Book() {{
title = "Real World Haskell";
author = "Don Stewart";
}});
Book book = mapper.readValue(json, Book.class);
assertEquals("Real World Haskell", book.title);
assertEquals("Don Stewart", book.author);
}
@Test
@SuppressWarnings("serial")
public void readWriteJsonArray() throws JsonGenerationException, JsonMappingException, IOException {
List<Book> books = new ArrayList<Book>() {{
add(new Book() {{
title = "Real World Haskell";
author = "Don Stewart";
}});
add(new Book() {{
title = "Learn You Some Erlang";
author = "Fred T. Herbert";
}});
}};
String json = mapper.writeValueAsString(books);
books = mapper.readValue(json, new TypeReference<List<Book>>() {});
assertEquals("Real World Haskell", books.get(0).title);
assertEquals("Don Stewart", books.get(0).author);
assertEquals("Learn You Some Erlang", books.get(1).title);
assertEquals("Fred T. Herbert", books.get(1).author);
}
}
答案 2 :(得分:0)
我不知道杰克逊我害怕,但是有可能在Cars
类中有一个构造函数,它将参数作为JSON数组的一个元素(无论是哪种类型)在杰克逊)。然后,构造函数将从JSON汽车条目的每个键中提取值,并根据需要填充类成员。
答案 3 :(得分:0)
我使用Gson库(http://code.google.com/p/google-gson/)做了类似的事情。我发现它比杰克逊更容易使用。
这是一个解析联系人响应的示例,看起来它可以很好地转换为您的汽车示例:
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
...
public class TestContact implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("Name") private String mName;
@SerializedName("PhoneNumber") private String mPhoneNumber;
@SerializedName("Address1") private String mAddress1;
@SerializedName("Address2") private String mAddress2;
@SerializedName("City") private String mCity;
@SerializedName("State") private String mState;
@SerializedName("Zip") private String mZip;
@SerializedName("Website") private String mWebsite;
@SerializedName("Email") private String mEmail;
public static TestContact create(JSONObject response) throws JSONException {
Gson gson = new Gson();
TestContact contact = gson.fromJson(response.toString(), TestContact.class);
return contact;
}
public static ArrayList<TestContact> createList(JSONObject response) throws JSONException {
ArrayList<TestContact> contacts = new ArrayList<TestContact>();
JSONArray contactResponses = response.getJSONArray("Contacts");
for (int i = 0; i < contactResponses.length(); i++) {
JSONObject contactResponse = contactResponses.getJSONObject(i);
contacts.add(create(contactResponse));
}
return contacts;
}
...
}