亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 GSON 反序列化嵌套的 JSON 數組?

如何使用 GSON 反序列化嵌套的 JSON 數組?

慕森王 2022-09-14 10:33:36
這是我的一個例子: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);
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您不能跳過根目錄 。在這種情況下,最簡單的解決方案是 - 創建根:JSON objectPOJO


class Response {

    @SerializedName("value")

    private List<Customer> customers;


    // getters, setters

}

您可以按如下方式使用它:


return gson.fromJson(json, Response.class).getCustomers();


查看完整回答
反對 回復 2022-09-14
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

您不必擔心編寫自己的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")

@Expose

private String status;

@SerializedName("rowCount")

@Expose

private Integer rowCount;

@SerializedName("pageCount")

@Expose

private Integer pageCount;

@SerializedName("value")

@Expose

private 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")

@Expose

private Integer customerID;

@SerializedName("CustomerTypeID")

@Expose

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

}


}

以上兩類都是網站自動生成的。


查看完整回答
反對 回復 2022-09-14
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

import java.util.List;

import com.google.gson.annotations.Expose;

import com.google.gson.annotations.SerializedName;


 public class ExampleClass {


 @SerializedName("status")

 @Expose

 private String status;

 @SerializedName("rowCount")

 @Expose

 private int rowCount;

 @SerializedName("pageCount")

 @Expose

 private int pageCount;

 @SerializedName("value")

 @Expose

 private List<Value> value = null;


 public String getStatus() {

 return status;

  }


 public void setStatus(String status) {

this.status = status;

 }


 public int getRowCount() {

 return rowCount;

  }


  public void setRowCount(int rowCount) {

 this.rowCount = rowCount;

 }


 public int getPageCount() {

   return pageCount;

   }


 public void setPageCount(int pageCount) {

   this.pageCount = pageCount;

   }


  public List<Value> getValue() {

 return value;

 }


  public void setValue(List<Value> value) {

 this.value = value;

  }


 }

-----------------------------------價值.java-----------------------------------


 import com.google.gson.annotations.Expose;

 import com.google.gson.annotations.SerializedName;


 public class Value {


 @SerializedName("CustomerID")

 @Expose

 private int customerID;

 @SerializedName("CustomerTypeID")

 @Expose

 private int customerTypeID;


 public int getCustomerID() {

 return customerID;

}


 public void setCustomerID(int customerID) {

this.customerID = customerID;

}


public int getCustomerTypeID() {

return customerTypeID;

}


public void setCustomerTypeID(int customerTypeID) {

this.customerTypeID = customerTypeID;

 }


 }

/****** 解析與格森 ******/


    GsonBuilder gsonBuilder = new GsonBuilder(); 

    gson = gsonBuilder.create(); 

    ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);

    List<Value> yourListOfCustomerValues = resultObj.getValue();

您可以參考這篇關于諾曼·佩特克的Gson的數組映射和對象列表的驚人帖子

Gson 的基礎知識、模型注釋和嵌套對象的映射


查看完整回答
反對 回復 2022-09-14
  • 3 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號