我正在使用翻新来检索json对象。但是,我想知道是否有一种简单的方法来检索嵌套对象。
这是我的JSON字符串:
{
"name": "125 8th avenue",
"address": "125 8th avenue, San fran ,CA 09012",
"location": {
"lon": -72.98013329999998,
"lat": 45.7552112
},
"email": "support@email.com",
"primaryContact": {
"firstName": "john",
"lastName": "doe",
"jobTitle": "General Manager, 8th Ave",
"email": "support@email.com",
"photo": "//images.ctfassets.net/qykmdxxsgb04/3EaIeJ29djgo6Exve4Q7xb.jpeg"
}
我要检索的名称和电子邮件为:
@Expose
@SerializedName("name")
private String name;
@Expose
@SerializedName("email")
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MyInfo)) return false;
MyInfo that = (MyInfo) o;
if (!name.equals(that.name)) return false;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + email.hashCode();
return result;
}
从JSON中可以看到,它很容易检索名称和电子邮件,但不确定如何在同一文件中轻松检索primaryContact详细信息(例如名字和姓氏)?有什么想法吗?
预先感谢
答案 0 :(得分:1)
您还必须创建primaryContact
对象,并使用@Expose
和@SerializedName("whatever")
以相同的方式对其进行序列化。然后将primaryContact
添加到您拥有的类中,并使用正确的名称对其进行序列化。
与json的嵌套方式基本相同。因此,您可以嵌套对象,而不是嵌套的JSON。
答案 1 :(得分:1)
两种方式:
将联系人和位置设置为内部类(相同的文件),但是仍然很难从外部访问这些字段。
您可以创建一种方法来从“地址”访问联系人中的属性。
我使用http://www.jsonschema2pojo.org/自动生成以下文件
public class Address {
// create a method here to get first/last name
public String getFirstName(){
return primaryContact==null? "" :
primaryContact.getFirstName();
}
// do the same for which ever inner attributes you like to access.
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
@SerializedName("location")
@Expose
private Location location;
@SerializedName("email")
@Expose
private String email;
@SerializedName("primaryContact")
@Expose
private PrimaryContact primaryContact;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public PrimaryContact getPrimaryContact() {
return primaryContact;
}
public void setPrimaryContact(PrimaryContact primaryContact) {
this.primaryContact = primaryContact;
}
}
-----------------------------------com.example.Location.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Location {
@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("lat")
@Expose
private Double lat;
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
}
-----------------------------------com.example.PrimaryContact.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PrimaryContact {
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("jobTitle")
@Expose
private String jobTitle;
@SerializedName("email")
@Expose
private String email;
@SerializedName("photo")
@Expose
private String photo;
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 getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}