我无法正确生成能够兑换 JSON 日期的 kotlin 对象

Posted

技术标签:

【中文标题】我无法正确生成能够兑换 JSON 日期的 kotlin 对象【英文标题】:I can't properly generate a kotlin object capable of redeeming the JSON date 【发布时间】:2021-12-07 21:49:01 【问题描述】:

那么我应该如何编写我的 Evolution 类,以便它在服务器响应时工作并收集如此多的 JSON 数据?

我的问题出在“清单”这个字段中

JSON:


  "message": "Consulta realizada com sucesso",
  "data": [
    
      "id": 120,
      "tipo_evolucao_id": 5,
      "tipo_evolucao": "Frequência Cardíaca",
      "data_hora": "20\/10\/2021 às 13:39",
      "responsavel": "Vinicius Marconi Vasconcelos Berni",
      "evolucao": "Paciente vem apresentando resistência na administração dos medicamentos.",
      "checklists": 
        "Frequência Cardíaca": 
          "label": "Frequência Cardíaca",
          "itens": [
            
              "pergunta": "Frequência observada",
              "resposta": "1"
            
          ]
        
      
    ,
    
      "id": 111,
      "tipo_evolucao_id": 1,
      "tipo_evolucao": "Necessidades Fisiológicas",
      "data_hora": "19\/10\/2021 às 11:52",
      "responsavel": "Vinicius Marconi Vasconcelos Berni",
      "evolucao": "foi foi e foi",
      "checklists": 
        "Evacuação": 
          "label": "Evacuação",
          "itens": [
            
              "pergunta": "Tipo",
              "resposta": "\"liquida\""
            ,
            
              "pergunta": "Quantidade",
              "resposta": "\"normal\""
            
          ]
        
      
    
    ]

我的科特林课堂:

class Evolution 
    @SerializedName("message")
    @Expose
    var message: String? = null

    @SerializedName("data")
    @Expose
    var data: List<EvoData>? = null

class EvoData 
    @SerializedName("id")
    @Expose
    var id: Int? = null

    @SerializedName("tipo_evolucao_id")
    @Expose
    var tipoEvolucaoId: Int? = null

    @SerializedName("tipo_evolucao")
    @Expose
    var tipoEvolucao: String? = null

    @SerializedName("data_hora")
    @Expose
    var dataHora: String? = null

    @SerializedName("responsavel")
    @Expose
    var responsavel: String? = null

    @SerializedName("evolucao")
    @Expose
    var evolucao: String? = null

    @SerializedName("checklists")
    @Expose
    var checklists: CheckLists? = null

class CheckLists 

    @Expose
    var checklist:  Checklist? = null


class Checklist 
    @SerializedName("label")
    @Expose
    var label: String? = null


    @SerializedName("itens")
    @Expose
    var itens: List<Itens>? = null


class Itens 

    @SerializedName("pergunta")
    @Expose
    var pergunta: String? = null

    @SerializedName("resposta")
    @Expose
    var resposta: String? = null

日志错误:

D/erro: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 1132 path $.data[2].checklists

那么我应该如何编写我的 Evolution 类,以便它在服务器响应时工作并收集如此多的 JSON 数据?

【问题讨论】:

【参考方案1】:

checklists JSON 属性是一个 Map,因此请尝试以下操作:

class EvoData 
    @SerializedName("id")
    @Expose
    var id: Int? = null

    @SerializedName("tipo_evolucao_id")
    @Expose
    var tipoEvolucaoId: Int? = null

    @SerializedName("tipo_evolucao")
    @Expose
    var tipoEvolucao: String? = null

    @SerializedName("data_hora")
    @Expose
    var dataHora: String? = null

    @SerializedName("responsavel")
    @Expose
    var responsavel: String? = null

    @SerializedName("evolucao")
    @Expose
    var evolucao: String? = null

    @SerializedName("checklists")
    @Expose
    var checklists: Map<String, Checklist> = emptyMap()

您可以删除您的CheckLists 课程。

【讨论】:

@João Gabriel 有帮助吗?

以上是关于我无法正确生成能够兑换 JSON 日期的 kotlin 对象的主要内容,如果未能解决你的问题,请参考以下文章

水壶 - JSON 输入在无法解析的日期失败

如何使用 jOOQ 通过 SQLite 正确生成日期和时间类型

Grails 无法将 JSON 中的日期/时间解组回 joda DateTime

jq生成的Shell命令无法正确运行

PHFetchResults 日期过滤器没有为时间范围生成正确的结果

C++ Qt 无法正确解析 JSON 数组