我無法解析JSON字符串。下面的JSON給出:JSON - 對象數組內的對象數組
[
{
"firstname": "firstname",
"lastname": "lastname",
"address": "address",
"images": [
{
"image": {
"url": "url",
"id": "id"
}
},
{
"image": {
"url": "url",
"id": "id"
}
}
]
},
{
"firstname": "firstname",
"lastname": "lastname",
"address": "address",
"images": [
{
"image": {
"url": "url",
"id": "id"
}
},
{
"image": {
"url": "url",
"id": "id"
}
}
]
}
]
我已經定義所需的豆類如: Person.java
public class Person implements Serializable {
private static final long serialVersionUID = 38L;
private String firstname;
private String lastname;
private String address;
private Image[] images;
public Person() {
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Image[] getImages() {
return images;
}
public void setImages(Image[] images) {
this.images = images;
}
}
Image.java:
public class Image implements Serializable {
private static final long serialVersionUID = 39L;
private String url;
private String id;
public Image() {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
現在我解析JSON字符串作爲:
Gson gson = new Gson();
Person[] persons = (Person[])gson.fromJson(jsonString, Person[].class);
現在如果我打印
System.out.println(persons[0].getFirstname());
System.out.println(persons[0].getLastname());
System.out.println(persons[0].getAddress());
它正在打印相應的值。另外:
persons[0].getImages() is not null;
persons[0].getImages()[0] is not null;
但
persons[0].getImages()[0].getUrl() is null;
persons[0].getImages()[0].getId() is null;
我無法明白我做錯了什麼?我在定義豆類時是否有任何錯誤?
我真的很感謝你的幫助。
謝謝。你讓我今天一整天都感覺很好。 – 2012-02-23 14:02:03