我正在从我正在尝试解析的服务器收到JSON响应。获取实际属性很简单,但我在将响应中的对象解析为数组时遇到问题,然后我可以将其解析为表格进行显示。
{
"response": "User exists",
"data": {
"id": 5,
"list": 7,
"first_name": "Aaron",
"last_name": "Test",
"email": "testing@testdomaij.com",
"custom_fields": {
"Is this a question?": "This is an answer",
"Test Question": "Test answer!"
},
"created_at": "Oct 14, 2015"
}
}
我已经将数据对象传递给POJO并且能够获取所有字符串和整数,但我不确定如何将custom_fields对象解析为数组。每个用户的键和值都不同,我需要找到一种方法来显示它们。
我解析数据的当前User对象如下:
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
public class User implements Parcelable {
private String id;
private String list;
@SerializedName("first_name")
private String firstName;
@SerializedName("last_name")
private String lastName;
private String email;
@SerializedName("custom_fields")
private CustomFields customFields;
@SerializedName("created_at")
private String createdAt;
public User() {
}
public User(Parcel source) {
id = source.readString();
list = source.readString();
firstName = source.readString();
lastName = source.readString();
email = source.readString();
createdAt = source.readString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getList() {
return list;
}
public void setList(String list) {
this.list = list;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void getCustomFields() {
// I have no idea how to parse and return as an array
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(list);
dest.writeString(firstName);
dest.writeString(lastName);
dest.writeString(email);
dest.writeString(createdAt);
}
public static Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
}
如何解析custom_fields对象的键和值并将它们作为我可以使用的数组返回?