Android 无法将JSON解析为对象列表
很难将其解析到我的对象中 答复:Android 无法将JSON解析为对象列表,android,gson,retrofit2,Android,Gson,Retrofit2,很难将其解析到我的对象中 答复: { "status": "OK", "results": [ { "num_results": "102", "members": [ { "id": "A000062", "first_name": "George", "office": "", "phone": "",
{
"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": ""
}
]
}
]
}
反序列化器类
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");
}
但我不知道从这里到哪里去结束成员的集合。有人能给我指出一个好的方向吗?你有没有尝试过像这样的生成工具 其输出为:
-----------------------------------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;
}
------------------------------------com.example2.Member.java-----------------------------------
包com.example2;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共班级成员{
@序列化名称(“id”)
@暴露
私有字符串id;
@SerializedName(“名字”)
@暴露
私有字符串名;
@序列化名称(“办公室”)
@暴露
私人字符串办公室;
@序列化名称(“电话”)
@暴露
私人电话;
@序列化名称(“传真”)
@暴露
私人字符串传真;
@序列化名称(“状态”)
@暴露
私有字符串状态;
@序列化名称(“lis_id”)
@暴露
私有字符串lisId;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getOffice(){
返回办公室;
}
公共办公室(字符串办公室){
这个办公室=办公室;
}
公共字符串getPhone(){
回电话;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
公共字符串getFax(){
回传传真;
}
公用传真(字符串传真){
this.fax=传真;
}
公共字符串getState(){
返回状态;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共字符串getLisId(){
返回lisId;
}
公共无效setLisId(字符串lisId){
this.lisId=lisId;
}
}
-----------------------------------com.example2.Members.java-----------------------------------
包com.example2;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共班级成员{
@序列化名称(“状态”)
@暴露
私有字符串状态;
@SerializedName(“结果”)
@暴露
私有列表结果=null;
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共列表getResults(){
返回结果;
}
公共void setResults(列出结果){
这个结果=结果;
}
}
-----------------------------------com.example2.Result.java-----------------------------------
包com.example2;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课成绩{
@SerializedName(“num_结果”)
@暴露
私有字符串numResults;
@序列化名称(“成员”)
@暴露
私有列表成员=null;
公共字符串getNumResults(){
返回numResults;
}
公共void setNumResults(字符串numResults){
this.numResults=numResults;
}
公共列表getMembers(){
返回成员;
}
公共成员(列表成员){
这个.成员=成员;
}
您只需要创建一个JSON映射,这里似乎不需要自定义反序列化程序。您说得对,对不起。我已经用Gson更新了答案。
-----------------------------------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;
}