使用 Retrofit 解析数组内的动态数组

Posted

技术标签:

【中文标题】使用 Retrofit 解析数组内的动态数组【英文标题】:Parse dynamic array inside array using Retrofit 【发布时间】:2015-12-06 07:23:02 【问题描述】:

我正在尝试使用 Retrofit 解析下面的数据结构,该结构在另一个动态数组集合中具有动态数组集合。

模型类是否正常或服务器响应有问题?

结构#


  "DATA": 
    "2015": [
      [

      ],
      [

      ],
      [

      ],
      [

      ],
      [

      ],
      [
        
          "fAmount": "1000",
          "paydate": "07\/29\/2015",
          "vName": "Mr. mule",
          "vPropertyStreet": "Street light",
          "vPropertyState": "test2",
          "vPropertyZipcode": "2222222",
          "vPropertyCity": "test2",
          "type": "Credit",
          "vPdfURL": "url"
        ,
        
          "fAmount": "1000",
          "paydate": "07\/29\/2015",
          "vName": "new",
          "vPropertyStreet": "Street light",
          "vPropertyState": "test2",
          "vPropertyZipcode": "2222222",
          "vPropertyCity": "test2",
          "type": "Charge",
          "vPdfURL": "url"
        ,
        
          "fAmount": "1000",
          "paydate": "07\/29\/2015",
          "vName": "Mr. mule",
          "vPropertyStreet": "Street light",
          "vPropertyState": "test2",
          "vPropertyZipcode": "2222222",
          "vPropertyCity": "test2",
          "type": "Credit",
          "vPdfURL": "url"
        
      ],
      [

      ],
      [

      ],
      [

      ],
      [

      ],
      [

      ],
      [

      ]
    ]
  ,
  "SUCCESS": 1

错误#

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT

试过#

public class DataModelUserStatement 
    @SerializedName("DATA")
    Map<String, List<DataModelUserStatementSub>> types;

    Integer SUCCESS;

    String MESSAGE;

    public Map<String, List<DataModelUserStatementSub>> getTypes() 
        return types;
    

    public void setTypes(Map<String, List<DataModelUserStatementSub>> types) 
        this.types = types;
    

    public Integer getSUCCESS() 
        return SUCCESS;
    

    public void setSUCCESS(Integer SUCCESS) 
        this.SUCCESS = SUCCESS;
    

    public String getMESSAGE() 
        return MESSAGE;
    

    public void setMESSAGE(String MESSAGE) 
        this.MESSAGE = MESSAGE;
    


 public class DataModelUserStatementSub 
        Map<String, List<Type>> types;


        public Map<String, List<Type>> getTypes() 
            return types;
        

        public void setTypes(Map<String, List<Type>> types) 
            this.types = types;
        
    

public class Type 
    @SerializedName("fAmount") private String fAmount;
    @SerializedName("paydate") private String paydate;
    @SerializedName("vName") private String vName;
    @SerializedName("vPropertyStreet") private String vPropertyStreet;
    @SerializedName("vPropertyState") private String vPropertyState;
    @SerializedName("vPropertyZipcode") private String vPropertyZipcode;
    @SerializedName("vPropertyCity") private String vPropertyCity;
    @SerializedName("type") private String type;
    @SerializedName("vPdfURL") private String vPdfURL;


    public String getType() 
        return type;
    

    public void setType(String type) 
        this.type = type;
    

    public String getvPdfURL() 
        return vPdfURL;
    

    public void setvPdfURL(String vPdfURL) 
        this.vPdfURL = vPdfURL;
    

    public String getfAmount() 
        return fAmount;
    

    public void setfAmount(String fAmount) 
        this.fAmount = fAmount;
    

    public String getPaydate() 
        return paydate;
    

    public void setPaydate(String paydate) 
        this.paydate = paydate;
    

    public String getvName() 
        return vName;
    

    public void setvName(String vName) 
        this.vName = vName;
    

    public String getvPropertyStreet() 
        return vPropertyStreet;
    

    public void setvPropertyStreet(String vPropertyStreet) 
        this.vPropertyStreet = vPropertyStreet;
    

    public String getvPropertyState() 
        return vPropertyState;
    

    public void setvPropertyState(String vPropertyState) 
        this.vPropertyState = vPropertyState;
    

    public String getvPropertyZipcode() 
        return vPropertyZipcode;
    

    public void setvPropertyZipcode(String vPropertyZipcode) 
        this.vPropertyZipcode = vPropertyZipcode;
    

    public String getvPropertyCity() 
        return vPropertyCity;
    

    public void setvPropertyCity(String vPropertyCity) 
        this.vPropertyCity = vPropertyCity;
    

【问题讨论】:

【参考方案1】:

最后我实现了它的工作,模型类有问题。

我们只需要使用下面两个模型类。


DataModelUserStatement.class

public class DataModelUserStatement 
    @SerializedName("DATA")
    Map<String, List<List<Type>>> types;

    Integer SUCCESS;

    String MESSAGE;

    public Map<String, List<List<Type>>> getTypes() 
        return types;
    

    public void setTypes(Map<String, List<List<Type>>> types) 
        this.types = types;
    

    public Integer getSUCCESS() 
        return SUCCESS;
    

    public void setSUCCESS(Integer SUCCESS) 
        this.SUCCESS = SUCCESS;
    

    public String getMESSAGE() 
        return MESSAGE;
    

    public void setMESSAGE(String MESSAGE) 
        this.MESSAGE = MESSAGE;



    

还有我在Question中发布的Type

【讨论】:

以上是关于使用 Retrofit 解析数组内的动态数组的主要内容,如果未能解决你的问题,请参考以下文章

Retrofit源码解析-动态代理

如何使用Retrofit 2解析动态JSON(+嵌套对象)

使用 Retrofit 解析动态密钥 Json 字符串

如何使用 Retrofit 从 Json 数组列表中检索 MpAndroid 图表内的数据?

使用 Retrofit 解析带有对象数组的 JSON 数组

为具有双指针的结构内的动态结构数组分配内存**