如何通过改造检索嵌套的 json 对象?
how to retrieve nested json object with retrofit?
我正在使用改装来检索 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
对象并以与 @Expose
和 @SerializedName("whatever")
相同的方式对其进行序列化。然后将 primaryContact
添加到您拥有的 class 并使用正确的名称对其进行序列化。
它与 json 的嵌套方式基本相同。所以不是嵌套 JSON,而是嵌套对象。
两种方式:
将联系人和位置设置为内部 class(同一文件),但仍然无法从外部轻松访问这些字段。
您可以改为创建一个方法来访问 Address 中联系人中的属性。
我使用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;
}
}
我正在使用改装来检索 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
对象并以与 @Expose
和 @SerializedName("whatever")
相同的方式对其进行序列化。然后将 primaryContact
添加到您拥有的 class 并使用正确的名称对其进行序列化。
它与 json 的嵌套方式基本相同。所以不是嵌套 JSON,而是嵌套对象。
两种方式:
将联系人和位置设置为内部 class(同一文件),但仍然无法从外部轻松访问这些字段。
您可以改为创建一个方法来访问 Address 中联系人中的属性。
我使用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;
}
}