如何使用 GSON 反序列化嵌套的 JSON 数组?

这是我的一个例子:JSON


{

    "status": "ok",

    "rowCount": 60,

    "pageCount": 6,

    "value": [{

        "CustomerID": 1911,

        "CustomerTypeID": 3,

           ...

         }

       ]

}

我的手机:


@SerializedName("CustomerID")

public Integer CustomerID;


@SerializedName("CustomerTypeID")

public Integer CustomerTypeID;

我想把所有东西都拉到下面。value


如何使用谷歌的GSON来做到这一点?


我已经尝试像往常一样这样做,但由于显而易见的原因,它不起作用:


Type collectionType = new TypeToken<ArrayList<Customer>>() {}.getType();

return gson.fromJson(json, collectionType);


慕森王
浏览 149回答 3
3回答

回首忆惘然

您不能跳过根目录 。在这种情况下,最简单的解决方案是 - 创建根:JSON objectPOJOclass Response {&nbsp; &nbsp; @SerializedName("value")&nbsp; &nbsp; private List<Customer> customers;&nbsp; &nbsp; // getters, setters}您可以按如下方式使用它:return gson.fromJson(json, Response.class).getCustomers();

慕田峪7331174

您不必担心编写自己的POJO。只需访问 http://www.jsonschema2pojo.org/ 并将您的JSON数据粘贴到此处,它将自动返回您转换后的类,如下所示-----------------------------------示例.java-----------------------------------package com.example;import java.util.List;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Example {@SerializedName("status")@Exposeprivate String status;@SerializedName("rowCount")@Exposeprivate Integer rowCount;@SerializedName("pageCount")@Exposeprivate Integer pageCount;@SerializedName("value")@Exposeprivate List<Value> value = null;public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public Integer getRowCount() {return rowCount;}public void setRowCount(Integer rowCount) {this.rowCount = rowCount;}public Integer getPageCount() {return pageCount;}public void setPageCount(Integer pageCount) {this.pageCount = pageCount;}public List<Value> getValue() {return value;}public void setValue(List<Value> value) {this.value = value;}}-----------------------------------.java-----------------------------------package com.example;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Value {@SerializedName("CustomerID")@Exposeprivate Integer customerID;@SerializedName("CustomerTypeID")@Exposeprivate Integer customerTypeID;public Integer getCustomerID() {return customerID;}public void setCustomerID(Integer customerID) {this.customerID = customerID;}public Integer getCustomerTypeID() {return customerTypeID;}public void setCustomerTypeID(Integer customerTypeID) {this.customerTypeID = customerTypeID;}}以上两类都是网站自动生成的。

忽然笑

import java.util.List;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;&nbsp;public class ExampleClass {&nbsp;@SerializedName("status")&nbsp;@Expose&nbsp;private String status;&nbsp;@SerializedName("rowCount")&nbsp;@Expose&nbsp;private int rowCount;&nbsp;@SerializedName("pageCount")&nbsp;@Expose&nbsp;private int pageCount;&nbsp;@SerializedName("value")&nbsp;@Expose&nbsp;private List<Value> value = null;&nbsp;public String getStatus() {&nbsp;return status;&nbsp; }&nbsp;public void setStatus(String status) {this.status = status;&nbsp;}&nbsp;public int getRowCount() {&nbsp;return rowCount;&nbsp; }&nbsp; public void setRowCount(int rowCount) {&nbsp;this.rowCount = rowCount;&nbsp;}&nbsp;public int getPageCount() {&nbsp; &nbsp;return pageCount;&nbsp; &nbsp;}&nbsp;public void setPageCount(int pageCount) {&nbsp; &nbsp;this.pageCount = pageCount;&nbsp; &nbsp;}&nbsp; public List<Value> getValue() {&nbsp;return value;&nbsp;}&nbsp; public void setValue(List<Value> value) {&nbsp;this.value = value;&nbsp; }&nbsp;}-----------------------------------价值.java-----------------------------------&nbsp;import com.google.gson.annotations.Expose;&nbsp;import com.google.gson.annotations.SerializedName;&nbsp;public class Value {&nbsp;@SerializedName("CustomerID")&nbsp;@Expose&nbsp;private int customerID;&nbsp;@SerializedName("CustomerTypeID")&nbsp;@Expose&nbsp;private int customerTypeID;&nbsp;public int getCustomerID() {&nbsp;return customerID;}&nbsp;public void setCustomerID(int customerID) {this.customerID = customerID;}public int getCustomerTypeID() {return customerTypeID;}public void setCustomerTypeID(int customerTypeID) {this.customerTypeID = customerTypeID;&nbsp;}&nbsp;}/****** 解析与格森 ******/&nbsp; &nbsp; GsonBuilder gsonBuilder = new GsonBuilder();&nbsp;&nbsp; &nbsp; gson = gsonBuilder.create();&nbsp;&nbsp; &nbsp; ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);&nbsp; &nbsp; List<Value> yourListOfCustomerValues = resultObj.getValue();您可以参考这篇关于诺曼·佩特克的Gson的数组映射和对象列表的惊人帖子Gson 的基础知识、模型注释和嵌套对象的映射
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java