Reputation: 29816
I am having trouble to parse a JSON string. The JSON is given below:
[
{
"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"
}
}
]
}
]
I have defined the required Beans as: 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;
}
}
Now I am parsing the JSON string as:
Gson gson = new Gson();
Person[] persons = (Person[])gson.fromJson(jsonString, Person[].class);
Now if I print
System.out.println(persons[0].getFirstname());
System.out.println(persons[0].getLastname());
System.out.println(persons[0].getAddress());
it is printing the corresponding values. Also:
persons[0].getImages() is not null;
persons[0].getImages()[0] is not null;
but
persons[0].getImages()[0].getUrl() is null;
persons[0].getImages()[0].getId() is null;
I am unable to understand what I am doing wrong? Is there any mistake I made in defining the beans?
I really appreciate your help.
Upvotes: 1
Views: 2452
Reputation: 4637
You'll need another object to contain the image such that
class Image {
String image; Info info;
}
class Info {
String url, String id;
}
Upvotes: 1
Reputation: 1665
I assume that you shouldn't specify name of object inside the array. Replace
"image": {
"url": "url",
"id": "id"
}
with simply
{
"url": "url",
"id": "id"
}
In case you want to parse specified JSON String you should have differen Object structure:
public class Person implements Serializable {
private static final long serialVersionUID = 38L;
private String firstname;
private String lastname;
private String address;
private Foo[] images;
}
public class Foo{
private Image image;
}
public class Image implements Serializable {
private static final long serialVersionUID = 39L;
private String url;
private String id;
}
Upvotes: 3
Reputation: 1157
I think that probably you want your JSON to look more like
[
{
"firstname": "firstname",
"lastname": "lastname",
"address": "address",
"images": [
{
"url": "url",
"id": "id"
}...
That is, it might be getting confused by the "image" tag within the "images" array
Upvotes: 1