【问题标题】:Not able to fetch data correctly from Google Sheet无法从 Google 表格中正确获取数据
【发布时间】:2020-09-25 15:15:40
【问题描述】:

我设法从 Google Sheet 获取 JSON 数据,但它导致了一个致命错误:

致命错误:“试试!”表达式意外引发错误: Swift.DecodingError.keyNotFound(编码键(字符串值: "totalmarketvalue", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "否 与键 CodingKeys(stringValue: "totalmarketvalue", intValue: nil) ("totalmarketvalue").", 底层错误:无)):文件

下面列出了我的结构和函数

struct PostTotal: Codable {
    // let id = UUID()
    var rows: [Layer0]
    struct Layer0: Codable, Hashable {
        var totalmarketvalue: String
        var totaltodaysprofit: String
        var todaysprofitpercent: String
        var totalasset: String
        var maxpurchasepower: String   
    }
}
func getTotalValue () -> String {
    let urlTotal = URL(string: webstringtotal)
    var results: String = ""
    URLSession.shared.dataTask(with: urlTotal!) { (datas, _, _) in
        let postss = try! JSONDecoder().decode(PostTotal.Layer0.self, from: datas!)
        results = postss.totalasset
    }
.resume()
    print(results)
    return results
}

【问题讨论】:

    标签: json swift google-sheets swiftui


    【解决方案1】:

    我相信你可以这样阅读这个错误:

    No value associated with key "totalmarketvalue"
    

    这意味着解码器在您的回复中找不到totalmarketvalue 键。

    您似乎正在尝试仅解码 PostTotal.Layer0

    let postss = try! JSONDecoder().decode(PostTotal.Layer0.self, from: datas!)
    

    但是,您的输入 JSON 在根目录中只有两个 CodingKey,即 columnsrows。当您尝试仅解码 PostTotal.Layer0 时,它需要 totalmarketvalue 键位于顶层。

    您必须解码整个 JSON 树,然后根据需要访问元素:

    let postss = try! JSONDecoder().decode(PostTotal.self, from: datas!)
    let layer0 = postss.rows
    

    备注

    如果您从某个外部资源中获取数据,则无法 100% 确定响应中是否包含该键。更好地使用do-try 并在丢失数据的情况下处理错误。

    请注意,您已指定 totalmarketvalue 类型为 String

    "totalmarketvalue": "100" // this will parse correctly
    "totalmarketvalue": 100 // this will fail
    

    您还可以明确指定CodingKeys 的类型。这将允许您将模型变量名称与响应模型分开。

    struct: Layer0: Codable, Hashable {
        enum CodingKeys: String, CodingKey { 
            case totalMarketValue = "totalmarketvalue"
            ...
        }
        var totalMarketValue: String
        ...
    }
    

    【讨论】:

    • “我的 JSON 文件结构”链接是我的 Google 表格的 JSON 结构。而且我确信 totalmarketvalue 类型是字符串。我发现有 intValue 是 nil。我不明白为什么 JSON 数据包含 Int Value....
    • 谢谢!我使用枚举,但它仍然显示 Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "totalmarketvalue", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \ "totalmarketvalue\", intValue: nil) (\"totalmarketvalue\").", underlyingError: nil)): file 里面有stringValue: "totalmarketvalue" 和intValue: nil,不知道这个intValue是怎么来的?
    • 请在解码之前分享您的datas 值。
    • “数据”只包含一行。 {"columns":{"totalmarketvalue":["43,072,400.00"],"totaltodaysprofit":["330000.00"],"todaysprofitpercent":["0.77%"],"totalasset":["48,442,250"],"maxpurchasepower" :["5,369,850.00"]},"rows":[{"totalmarketvalue":"43,072,400.00","totaltodaysprofit":"330000.00","todaysprofitpercent":"0.77%","totalasset":"48,442,250","maxpurchasepower" :"5,369,850.00"}]}
    • 谢谢,我更新了我的答案。简而言之,尝试解码 PostTotal.self 而不是 PostTotal.Layer0.self
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多