【问题标题】:error decoding array that was returned in json - swiftjson 中返回的错误解码数组 - swift
【发布时间】:2020-03-11 19:26:19
【问题描述】:

当试图解码对象数组“arquivo”的返回时,总是 nil

import UIKit

struct pessoa: Codable {
    let idPessoa: Int
    let nome: String
    let cpf: String
    let email: String
    let dtaNascimento: String
    let dtaCadastro: String
    let telefone: String
    let status: Int

}
struct arquivo: Codable {
    let idArquivo: Int
    let caminho: String
    let nome: String
    let descricao: String
    let tamanho: Double
    let idPessoa: Int
    let idOcorrencia: Int

    enum CodingKeys: String, CodingKey {
        case idArquivo
        case caminho
        case nome
        case descricao
        case tamanho
        case idPessoa
        case idOcorrencia
    }
}
struct PessoaDTO: Codable {
    let pessoa: pessoa!
    let arquivo: Array<arquivo>!

    enum CodingKeys: String, CodingKey {
        case pessoa = "pessoa"
        case arquivo = "arquivo"
    }
}

var json = """
{
"pessoa":
{
"idPessoa": 89,
"nome": "Arnaldo",
"cpf": "816.404.648-44",
"email": "testeocorrenciabus@google.com",
"dtaNascimento": "1969-01-01T03:00:00.000+0000",
"dtaCadastro": "2019-10-30T18:53:19.000+0000",
"telefone": "(54)58648-4464",
"status": 1,
"arquivo": [
{ "idArquivo": 91,
"caminho": "/images/u/1572461505454.jpg",
"nome": "1572461505454.jpg",
"descricao": "",
"tamanho": 31765,
"idPessoa": 89,
"idOcorrencia": 1
}
]
}
}
"""
do {
    let decodedBeerObject = try JSONDecoder().decode(PessoaDTO.self, from: json.data(using: .utf8)!)
    print(decodedBeerObject.pessoa)
    print(decodedBeerObject.arquivo)

} catch let error {
    print(error.localizedDescription)
}

结果:

可选(__lldb_expr_51.pessoa(idPessoa: 89, nome: "Arnaldo", cpf: “816.404.648-44”,电子邮件:“testeocorrenciabus@google.com”, dtaNascimento:“1969-01-01T03:00:00.000+0000”,dtaCadastro: “2019-10-30T18:53:19.000+0000”,电话:“(54)58648-4464”,状态: 1)) 无

【问题讨论】:

    标签: ios json swift decoding


    【解决方案1】:

    你的模型不正确

    arquivo 存在于pessoa 内部,不在同一级别。

    正确的解析代码是:

    struct Pessoa: Codable {
        let idPessoa: Int
        let nome: String
        let cpf: String
        let email: String
        let dtaNascimento: String
        let dtaCadastro: String
        let telefone: String
        let status: Int
        let arquivo: Array<Arquivo>!
    
    }
    struct Arquivo: Codable {
        let idArquivo: Int
        let caminho: String
        let nome: String
        let descricao: String
        let tamanho: Double
        let idPessoa: Int
        let idOcorrencia: Int
    }
    struct PessoaDTO: Codable {
        let pessoa: Pessoa!  
    
    }
    
    var json123 = """
    {
    "pessoa":
    {
    "idPessoa": 89,
    "nome": "Arnaldo",
    "cpf": "816.404.648-44",
    "email": "testeocorrenciabus@google.com",
    "dtaNascimento": "1969-01-01T03:00:00.000+0000",
    "dtaCadastro": "2019-10-30T18:53:19.000+0000",
    "telefone": "(54)58648-4464",
    "status": 1,
    "arquivo": [
    { "idArquivo": 91,
    "caminho": "/images/u/1572461505454.jpg",
    "nome": "1572461505454.jpg",
    "descricao": "",
    "tamanho": 31765,
    "idPessoa": 89,
    "idOcorrencia": 1
    }
    ]
    }
    }
    """
    do {
        let decodedBeerObject = try JSONDecoder().decode(PessoaDTO.self, from: json123.data(using: .utf8)!)
        print(decodedBeerObject.pessoa)
        print(decodedBeerObject.pessoa.arquivo)
    
    } catch let error {
        print(error.localizedDescription)
    }
    

    【讨论】:

    • 让 arquivo: [arquivo]
    【解决方案2】:

    你的结构有点错误,关键arquivoPessoa的一部分

    struct Pessoa: Codable {
        let idPessoa: Int
        let nome: String
        let cpf: String
        let email: String
        let arquivo: [Arquivo]
        let dtaNascimento: String
        let dtaCadastro: String
        let telefone: String
        let status: Int
    
    }
    struct Arquivo: Codable {
        let idArquivo: Int
        let caminho: String
        let nome: String
        let descricao: String
        let tamanho: Double
        let idPessoa: Int
        let idOcorrencia: Int
    }
    
    struct PessoaDTO: Codable {
        let pessoa: Pessoa
    }
    

    如果键名和结构成员匹配,则不需要编码键

    要得到真正的错误总是只打印error实例

    catch {
        print(error)
    }
    

    请以大写字母开头的结构命名。

    并且永远不要将 Codable 上下文中的结构成员声明为隐式展开的可选

    【讨论】:

    • 是的,我使用了第一个大写字母。这个只在操场上,我忘了放。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    相关资源
    最近更新 更多