【问题标题】:JSON - Array of Objects within a Array of ObjectsJSON - 对象数组中的对象数组
【发布时间】:2012-02-23 13:46:16
【问题描述】:

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

我已将所需的 Bean 定义为: 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 时犯了什么错误吗?

非常感谢您的帮助。

【问题讨论】:

    标签: java arrays json gson


    【解决方案1】:

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

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

    简单的

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

    如果你想解析指定的 JSON 字符串,你应该有不同的对象结构:

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

    【讨论】:

      【解决方案2】:

      我认为您可能希望您的 JSON 看起来更像

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

      也就是说,它可能会被“images”数组中的“image”标签混淆

      【讨论】:

      • 感谢您的回复,但我没有生成 JSON,我正在解析它。所以问题出在bean定义中!我该如何纠正它?
      • 我不确定。我认为您需要在“图像”对象中包含一个“图像”对象,但这确实是混乱和令人困惑的设计。希望其他有更多经验的人可以向您展示一个解决此问题的好方法,但我的解决方案可能只是完成使用更细粒度的解析器的工作;例如Jackson 或者,当然,您可以在解析之前手动处理 jsonstring。
      【解决方案3】:

      你需要另一个对象来包含图像,这样

      class Image {
         String image; Info info;
      }
      
      class Info {
        String url, String id;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-11
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多