简体   繁体   English

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

[英]How do I deserialize a nested JSON array using GSON?

Here's an example of my JSON :这是我的JSON示例:

{
    "status": "ok",
    "rowCount": 60,
    "pageCount": 6,
    "value": [{
        "CustomerID": 1911,
        "CustomerTypeID": 3,
           ...
         }
       ]
}

My POJO:我的POJO:

@SerializedName("CustomerID")
public Integer CustomerID;

@SerializedName("CustomerTypeID")
public Integer CustomerTypeID;

I want to pull everything under value .我想把所有东西都拉到value之下。

How do I do this using Google's GSON?如何使用 Google 的 GSON 执行此操作?

I've tried doing it as I would normally, but for, obvious reasons, it didn't work:我试过像往常一样这样做,但由于显而易见的原因,它没有用:

Type collectionType = new TypeToken<ArrayList<Customer>>() {}.getType();
return gson.fromJson(json, collectionType);

You can not skip root JSON object .您不能跳过根JSON object The simplest solution in this case is - create root POJO :在这种情况下,最简单的解决方案是 - 创建根POJO

class Response {
    @SerializedName("value")
    private List<Customer> customers;

    // getters, setters
}

And you can use it as below:您可以按如下方式使用它:

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

You don't need to worry writing your own POJO.您无需担心编写自己的 POJO。

just visit http://www.jsonschema2pojo.org/ and paste here your JSON data, it'll automatically return you converted classes as below只需访问http://www.jsonschema2pojo.org/并在此处粘贴您的 JSON 数据,它会自动返回您转换后的类,如下所示

-----------------------------------com.example.Example.java----------------------------------- --------------------- com.example.Example.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;
}

}

-----------------------------------com.example.Value.java----------------------------------- ------------------------------------- com.example.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;
}

}

The above two classes are auto generated by website.以上两个类是由网站自动生成的。

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

 }

-----------------------------------Value.java----------------------------------- -------------------------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;
 }

 }

/********* parsing with Gson ******/ /********* 使用 Gson 解析 ******/

    GsonBuilder gsonBuilder = new GsonBuilder(); 
    gson = gsonBuilder.create(); 
    ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);
    List<Value> yourListOfCustomerValues = resultObj.getValue();

You can refer to this amazing post on mapping of arrays and lists of objects with Gson by Norman Peitek您可以参考 Norman Peitek 撰写的有关使用 Gson 映射数组和对象列表的精彩帖子

Basics of Gson, model annotations and mapping of nested objects Gson 基础,模型注释和嵌套对象映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM