有没有办法在android中解析这个get json响应

时间:2016-10-12 13:32:42

标签: android android-studio

{
"success":"true",
"contacts": {
"member" : [
"name" : "x";
"phone" : "43323284"
}
];
"invitation":[
{
"name":"y"
"phone":"78994993"
}
],
"invite":[
{
"name":"z"
"contact:"567896789"
}
]
}

这是我的回答,我很难解析这个问题。请建议我如何解析这种类型的json

2 个答案:

答案 0 :(得分:0)

首先,创建模型类。 和

Gson gson = new Gson();
ModelClass model = gson.fromJson(jsonString, ModelClass.class);

解析json有很多选项。有关详细信息:https://stackoverflow.com/a/31743324/7001152

答案 1 :(得分:0)

json不是有效的。我已经编辑了你的json。

{
"success":"true",
"contacts": {
"member" : [{"name" : "x","phone" : "43323284"}],
"invitation":[{"name":"y","phone":"78994993"}],
"invite":[{"name":"z","contact":"567896789"}]
}
}

构建模型类,如:

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("success")
@Expose
private String success;
@SerializedName("contacts")
@Expose
private Contacts contacts;

public String getSuccess() {
return success;
}

public void setSuccess(String success) {
this.success = success;
}

public Contacts getContacts() {
return contacts;
}

public void setContacts(Contacts contacts) {
this.contacts = contacts;
}

}


public class Contacts {

@SerializedName("member")
@Expose
private List<Member> member = new ArrayList<Member>();
@SerializedName("invitation")
@Expose
private List<Invitation> invitation = new ArrayList<Invitation>();
@SerializedName("invite")
@Expose
private List<Invite> invite = new ArrayList<Invite>();

public List<Member> getMember() {
return member;
}

public void setMember(List<Member> member) {
this.member = member;
}

public List<Invitation> getInvitation() {
return invitation;
}


public void setInvitation(List<Invitation> invitation) {
this.invitation = invitation;
}


public List<Invite> getInvite() {
return invite;
}


public void setInvite(List<Invite> invite) {
this.invite = invite;
}

}


public class Invitation {

@SerializedName("name")
@Expose
private String name;
@SerializedName("phone")
@Expose
private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

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

}


public class Invite {

@SerializedName("name")
@Expose
private String name;
@SerializedName("contact")
@Expose
private String contact;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}

}


public class Member {

@SerializedName("name")
@Expose
private String name;
@SerializedName("phone")
@Expose
private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

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

从json获取对象:

Example obj = new Gson().fromJson(jsonObject, Example.class);