用于这种json的pojo看起来如何?
{
"data": [
[
{
"id": 1,
"header": "PACKING INSTRUCTIONS",
"ProductType": 1,
"CatCode": "IC0001"
},
{
"id": 11,
"header": "ODOR",
"ProductType": 1,
"CatCode": "IC0011"
}
],
[
{
"id": 2,
"header": "Carton Markings",
"ParentID": 1,
"ProductType": 1,
"CatCode": "IC0002"
},
{
"id": 3,
"header": "Carton Quality, Squareness, Tape, Etc",
"ParentID": 1,
"ProductType": 1,
"CatCode": "IC0003"
}
],
[
{
"defectId": 1,
"InspectionChildId": 3,
"Defects": "Other",
"CatCode": "IC0003"
},
{
"defectId": 1,
"InspectionChildId": 4,
"Defects": "Other",
"CatCode": "IC0004"
}
],
[
{
"id": 1856,
"name": "A-Front"
},
{
"id": 1857,
"name": "B-Rear Panel"
},
{
"id": 1858,
"name": "C-Top Panel"
},
{
"id": 1859,
"name": "D-Side Panel"
},
{
"id": 1860,
"name": "E-Bottom"
},
{
"id": 1861,
"name": "F-Interior"
}
],
[
{
"id": 1862,
"name": "Minor"
},
{
"id": 1863,
"name": "Major"
},
{
"id": 1864,
"name": "Critical"
}
],
[
{
"id": 1,
"product": "Samsonite"
},
{
"id": 2,
"product": "Speck"
}
]
]
}
当前我的POJO看起来像这样-
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.PrimaryKey;
import java.util.List;
public class CheckedListData {
private List<List<Data>> dataList;
public List<List<Data>> getDataList() {
return dataList;
}
public void setDataList(List<List<Data>> dataList) {
this.dataList = dataList;
}
public static class Data {
private List<Defect> defectList;
private List<SubDefect> subDefects;
private List<DefectCategory> defectCategories;
private List<DefectLocation> defectLocations;
private List<DefectOptions> defectOptions;
private List<ProductDesc> productDesc;
public List<DefectOptions> getDefectOptions() {
return defectOptions;
}
public void setDefectOptions(List<DefectOptions> defectOptions) {
this.defectOptions = defectOptions;
}
public List<Defect> getDefectList() {
return defectList;
}
public void setDefectList(List<Defect> defectList) {
this.defectList = defectList;
}
public List<SubDefect> getSubDefects() {
return subDefects;
}
public void setSubDefects(List<SubDefect> subDefects) {
this.subDefects = subDefects;
}
public List<DefectCategory> getDefectCategories() {
return defectCategories;
}
public void setDefectCategories(List<DefectCategory> defectCategories) {
this.defectCategories = defectCategories;
}
public List<DefectLocation> getDefectLocations() {
return defectLocations;
}
public void setDefectLocations(List<DefectLocation> defectLocations) {
this.defectLocations = defectLocations;
}
public List<ProductDesc> getProductDesc() {
return productDesc;
}
public void setProductDesc(List<ProductDesc> productDesc) {
this.productDesc = productDesc;
}
//==================================Defects==========================================================
// @Entity
public static class Defect {
private String ProductType;
private String header;
private String CatCode;
// @PrimaryKey
// @NonNull
private String id;
public String getProductType() {
return ProductType;
}
public void setProductType(String productType) {
ProductType = productType;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getCatCode() {
return CatCode;
}
public void setCatCode(String catCode) {
CatCode = catCode;
}
// @NonNull
public String getId() {
return id;
}
public void setId(/*@NonNull*/ String id) {
this.id = id;
}
}
//==================================SubDefects==========================================================
// @Entity(foreignKeys = @ForeignKey(entity = Defect.class,
// parentColumns = "id",
// childColumns = "parent_id"))
public static class SubDefect {
// @ColumnInfo(name = "parent_id")
private int parentID;
private int productType;
private String header;
private String catCode;
// @PrimaryKey
// @NonNull
private int id;
public int getParentID() {
return parentID;
}
public void setParentID(int parentID) {
this.parentID = parentID;
}
public int getProductType() {
return productType;
}
public void setProductType(int productType) {
this.productType = productType;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getCatCode() {
return catCode;
}
public void setCatCode(String catCode) {
this.catCode = catCode;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//==================================DefectOptions==========================================================
// @Entity(foreignKeys = @ForeignKey(entity = SubDefect.class,
// parentColumns = "inspection_child_id",
// childColumns = "id"))
public static class DefectOptions {
private String catCode;
// @PrimaryKey
// @NonNull
private int defectId;
// @ColumnInfo(name = "inspection_child_id")
private int inspectionChildId;
private String defects;
public String getCatCode() {
return catCode;
}
public void setCatCode(String catCode) {
this.catCode = catCode;
}
public int getDefectId() {
return defectId;
}
public void setDefectId(int defectId) {
this.defectId = defectId;
}
public int getInspectionChildId() {
return inspectionChildId;
}
public void setInspectionChildId(int inspectionChildId) {
this.inspectionChildId = inspectionChildId;
}
public String getDefects() {
return defects;
}
public void setDefects(String defects) {
this.defects = defects;
}
}
//==================================DefectLocation==========================================================
// @Entity
public static class DefectLocation {
private String name;
// @PrimaryKey
// @NonNull
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//==================================DefectCategory==========================================================
// @Entity
public static class DefectCategory {
private String name;
// @PrimaryKey
// @NonNull
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//==================================Product==========================================================
public static class ProductDesc {
private int id;
private String product;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
}
}
}
但是当我尝试使用此代码将上面的json解析为上面的pojo时-
Type listType = new TypeToken<CheckedListData>() {}.getType();
CheckedListData data = new Gson().fromJson(response, listType);
数据对象始终为null。难道我做错了什么? pojo的格式不正确吗?还是我必须手动解析此json才能遍历整个json数组?
public class Response{
@SerializedName("data")
private List<DataItem> data;
public List<DataItem> getData(){
return data;
}
}
并创建一个内容如下的DataItem类
public class DataItem {
@SerializedName("ProductType")
private int productType;
@SerializedName("header")
private String header;
@SerializedName("CatCode")
private String catCode;
@SerializedName("id")
private int id;
public int getProductType(){
return productType;
}
public String getHeader(){
return header;
}
public String getCatCode(){
return catCode;
}
public int getId(){
return id;
}
}
希望有帮助!
dataList
重命名为data
,此外,您可以拥有一个类(例如Various
)来捕获所有可能的字段,例如]]@Data
public class DataHolder {
private List<List<Various>> data;
}