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

发布于 2025-01-08 05:40:25 字数 2703 浏览 0 评论 0原文

我在解析 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 时犯了任何错误吗?

我真的很感谢你的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2025-01-15 05:40:25

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

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

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;
}
眼眸 2025-01-15 05:40:25

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

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

是这样,它可能会被“image”数组中的“image”标签混淆

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

过气美图社 2025-01-15 05:40:25

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

class Image {
   String image; Info info;
}

class Info {
  String url, String id;
}

You'll need another object to contain the image such that

class Image {
   String image; Info info;
}

class Info {
  String url, String id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文