Android 将数组Gson提取到JSON
我使用Gson-to-Json从后端提取作为响应的Json数据 这是来自服务器的示例响应:Android 将数组Gson提取到JSON,android,arrays,json,gson,Android,Arrays,Json,Gson,我使用Gson-to-Json从后端提取作为响应的Json数据 这是来自服务器的示例响应: [{ "name": "person name", "data": [{ "phone": [ "98153304", "18290304", "17226965" ], "other_details1 ": "other details", "other
[{
"name": "person name",
"data": [{
"phone": [
"98153304",
"18290304",
"17226965"
],
"other_details1 ": "other details",
"other_details2": "other details"
}]
}]
我可以使用
@SerializedName("other_details1")
@Expose
private String other_details;
并创造他们的接受者和接受者
但我无法提取响应中的数组
有人能告诉我如何使用Gson到Json提取响应中的数组吗。如果此格式不正确,请致电相同的,我会将此告知后端团队
谢谢您可以使用生成模型/POJO类。然后您只需要访问getter/setter 检查屏幕截图:
您可以使用生成模型/POJO类。然后您只需要访问getter/setter 检查屏幕截图:
反序列化泛型集合的方法:
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
...
Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
反序列化泛型集合的方法:
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
...
Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
试试这个
Gson gson = new Gson;
PersonModel jsonObject = gson.fromJson(jsonInput, PersonModel.class);
jsonObject.getPhone().get(0).getName(); //0 is position as per your content size give position
PersonModel.java
public class PersonModel {
@SerializedName("name")
@Expose
private String name;
@SerializedName("data")
@Expose
private List<Datum> data = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
}
Datum.java
public class Datum {
@SerializedName("phone")
@Expose
private List<String> phone = null;
@SerializedName("other_details1 ")
@Expose
private String otherDetails1;
@SerializedName("other_details2")
@Expose
private String otherDetails2;
public List<String> getPhone() {
return phone;
}
public void setPhone(List<String> phone) {
this.phone = phone;
}
public String getOtherDetails1() {
return otherDetails1;
}
public void setOtherDetails1(String otherDetails1) {
this.otherDetails1 = otherDetails1;
}
public String getOtherDetails2() {
return otherDetails2;
}
public void setOtherDetails2(String otherDetails2) {
this.otherDetails2 = otherDetails2;
}
}
试试这个
Gson gson = new Gson;
PersonModel jsonObject = gson.fromJson(jsonInput, PersonModel.class);
jsonObject.getPhone().get(0).getName(); //0 is position as per your content size give position
PersonModel.java
public class PersonModel {
@SerializedName("name")
@Expose
private String name;
@SerializedName("data")
@Expose
private List<Datum> data = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
}
Datum.java
public class Datum {
@SerializedName("phone")
@Expose
private List<String> phone = null;
@SerializedName("other_details1 ")
@Expose
private String otherDetails1;
@SerializedName("other_details2")
@Expose
private String otherDetails2;
public List<String> getPhone() {
return phone;
}
public void setPhone(List<String> phone) {
this.phone = phone;
}
public String getOtherDetails1() {
return otherDetails1;
}
public void setOtherDetails1(String otherDetails1) {
this.otherDetails1 = otherDetails1;
}
public String getOtherDetails2() {
return otherDetails2;
}
public void setOtherDetails2(String otherDetails2) {
this.otherDetails2 = otherDetails2;
}
}
使用方法如下:
JSONObjectJSONArrayJSONObjectnew JSONArrayyour json字符串here.get0.getdata.get0.getother_details2
或者通过简单地传递特定数组的键来迭代json以获取映射并使用值进行检索。使用以下方法:
JSONObjectJSONArrayJSONObjectnew JSONArrayyour json字符串here.get0.getdata.get0.getother_details2
或者迭代json以获取映射,并通过简单地传递特定数组的键来使用值进行检索。格式正确,您如何检索我无法检索数组,其他所有我能够提取的细节都无法检索。如果我使用诸如array或JsonArray之类的数据类型,那么它也没有用。结果为空。请粘贴代码片段,以便我们能更好地帮助您。仅供参考,phone在数据中,因此您需要访问另一个对象中的对象以检索phone的值。格式正确您如何检索我无法检索阵列phone,其余所有其他详细信息我都可以提取。如果我使用诸如array或JsonArray之类的数据类型,那么它也没有用。结果为空。请粘贴代码片段,以便我们能更好地帮助您。仅供参考,手机在数据中,因此您需要访问另一个对象中的对象以检索手机的值。谢谢,我正在尝试这种方法。感谢您抽出时间访问Sanat。我一定会让你知道的。谢谢,我正在尝试这种方法。谢谢你抽出时间来萨纳特。我一定会让你知道的。谢谢你的时间,山姆的解决方案已经解决了我的问题。谢谢你的时间,山姆的解决方案已经解决了我的问题。