JSON - 对象数组中的对象数组

时间:2012-02-23 13:46:17

标签: java arrays json gson

我无法解析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"
        }
      }
    ]
  }
]

我已将所需的Beans定义为: 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;

我无法理解我做错了什么?我在定义bean时是否有任何错误?

我非常感谢你的帮助。

3 个答案:

答案 0 :(得分:3)

我假设您不应该在数组中指定对象的名称。替换

"image": {          
    "url": "url",
    "id": "id"
}

只需

{          
    "url": "url",
    "id": "id"
}

如果你想解析指定的JSON String,你应该有不同的Object结构:

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

答案 1 :(得分:1)

我想你可能希望你的JSON看起来更像

[
  {
    "firstname": "firstname",
    "lastname": "lastname",
    "address": "address",
    "images": [   
      {
          "url": "url",
          "id": "id"
      }...

也就是说,它可能会被“images”数组中的“image”标签弄糊涂了

答案 2 :(得分:1)

您需要另一个对象来包含图像

class Image {
   String image; Info info;
}

class Info {
  String url, String id;
}