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"
}
}
]
}
]
我已将所需的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您不应该指定数组内的对象名称。替换
为简单
如果您想解析指定的 JSON 字符串,您应该具有不同的对象结构:
I assume that you shouldn't specify name of object inside the array. Replace
with simply
In case you want to parse specified JSON String you should have differen Object structure:
我认为您可能希望 JSON 看起来更像
是这样,它可能会被“image”数组中的“image”标签混淆
I think that probably you want your JSON to look more like
That is, it might be getting confused by the "image" tag within the "images" array
您需要另一个对象来包含图像,以便
You'll need another object to contain the image such that