android - 将数组 Gson 提取为 JSON

标签 android arrays json gson

我正在使用 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/

相关文章:

android - Android如何确定应用程序是否在 "High Battery Use"下有 "Recent Location Requests"?

android - ADT Bundle for mac 已损坏

android - Activity 的 onPause 和 onResume 启动后无缘无故被调用

javascript - 如何在客户端中接收换行符分隔的 JSON?

java - Android 应用程序架构 - RxJava

javascript - 在 JavaScript 中将 Python 字符串转换为数组

php - 通过ajax将多个数组传递给javascript

c - Visual Studio C 中的动态数组

php - 如果 Row1 = 值 1,则更新其他行

javascript - Chrome 扩展程序可将图像替换为其他图像