试图将JSON解析为对象列表

时间:2017-04-16 23:25:21

标签: android gson retrofit2

很难将其解析为我的对象。

响应:

{
"status": "OK",
"results": [
    {
        "num_results": "102",
        "members": [
            {
                "id": "A000062",
                "first_name": "George",
                "office": "",
                "phone": "",
                "fax": "",
                "state": "VT",
                "lis_id": ""
            },
            {
                "id": "B000095",
                "first_name": "Raymond",
                "office": "",
                "phone": "",
                "fax": "",
                "state": "CT",
                "lis_id": ""
            }
        ]
    }
]

}

DeSerializer类

public class MyDeserializer<Member> implements JsonDeserializer<Member>
{
@Override
public Member deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException
{     
    JsonElement content = je.getAsJsonObject().get("results");


        System.out.println("something");

  }

内容JsonElement如下所示:

[{"num_results":"102","offset":"0","members":[
{"id":"A000062","first_name":"George","office":"","phone":"","fax":"","state"  :"VT","lis_id":""},
{"id":"B000095","first_name":"Raymond","office":"","phone":"","fax":"","state":"ND","lis_id":""}]}]

但我不知道从哪里到最后收集会员。有人能指出我的方向吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试过像this这样的生成工具?

这个输出是:

 -----------------------------------com.example2.Member.java-----------------------------------

package com.example2;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Member {

@SerializedName("id")
@Expose
private String id;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("office")
@Expose
private String office;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("fax")
@Expose
private String fax;
@SerializedName("state")
@Expose
private String state;
@SerializedName("lis_id")
@Expose
private String lisId;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getOffice() {
return office;
}

public void setOffice(String office) {
this.office = office;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getFax() {
return fax;
}

public void setFax(String fax) {
this.fax = fax;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getLisId() {
return lisId;
}

public void setLisId(String lisId) {
this.lisId = lisId;
}

}
-----------------------------------com.example2.Members.java-----------------------------------

package com.example2;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Members {

@SerializedName("status")
@Expose
private String status;
@SerializedName("results")
@Expose
private List<Result> results = null;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public List<Result> getResults() {
return results;
}

public void setResults(List<Result> results) {
this.results = results;
}

}
-----------------------------------com.example2.Result.java-----------------------------------

package com.example2;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("num_results")
@Expose
private String numResults;
@SerializedName("members")
@Expose
private List<Member> members = null;

public String getNumResults() {
return numResults;
}

public void setNumResults(String numResults) {
this.numResults = numResults;
}

public List<Member> getMembers() {
return members;
}

public void setMembers(List<Member> members) {
this.members = members;
}