gpt4 book ai didi

android - 将数组 Gson 提取为 JSON

转载 作者:行者123 更新时间:2023-11-29 15:00:03 26 4
gpt4 key购买 nike

我正在使用 Gson 到 Json 来提取来自后端的响应的 JSON 数据。

这是我来自服务器的示例响应:

[{
"name": "person name",
"data": [{
"phone": [
"98153304",
"18290304",
"17226965"
],
"other_details1 ": "other details",
"other_details2": "other details"
}]
}]

我可以通过使用从响应中提取其他详细信息

@SerializedName("other_details1")
@Expose
private String other_details;

并创建他们的 getter 和 setter。

但我无法提取响应中的数组。

谁能指导我如何使用 Gson 将响应中的数组提取到 Json。如果此格式不正确,请调用我,我会与后端团队沟通。

谢谢

最佳答案

试试这个

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;
}

}

数据.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;
}

}

关于android - 将数组 Gson 提取为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com