首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯威夫特·约翰逊KeyNotFound

斯威夫特·约翰逊KeyNotFound
EN

Stack Overflow用户
提问于 2020-06-27 23:18:43
回答 1查看 91关注 0票数 1

请帮助使这个工作,我一直试图弄清楚JSON和Swift一个星期,并面临这个问题到目前为止的5个小时。

错误接收到

错误在解码JSON keyNotFound时( CodingKeys(stringValue:"MP",intValue: nil),Swift.DecodingError.Context(codingPath:[],debugDescription:“无与密钥CodingKeys关联的值”(stringValue:"MP",intValue: nil) (“MP”)。“,underlyingError: 0))

年为零

代码语言:javascript
复制
struct UserDay: Codable {
    let MP: UserMP
    let WP: UserWP
}

struct UserMP: Codable {
    let M: [UserM]
    let S: [UserS]
}

struct UserM : Codable {
    let title: String
    let description: String
    let time: String
}

struct UserS : Codable {
    let title: String
    let description: String
    let time: String
}

struct UserWP: Codable {
    let WP: [WPData]
}

struct WPData: Codable {
    let title: String
    let values: [Int]
}
代码语言:javascript
复制
class LogDataHandler {
    public func grabJSONInfo(){
        guard let jsonURL = Bundle(for: type(of: self)).path(forResource: "newLogData", ofType: "json") else { return }
        
        guard let jsonString = try? String(contentsOf: URL(fileURLWithPath: jsonURL), encoding: String.Encoding.utf8) else { return }

//        print(jsonString)
        // Print Info for TESTING
        var year: UserDay?
        
        do {
            year = try JSONDecoder().decode(UserDay.self, from: Data(jsonString.utf8))
        } catch {
            print("ERROR WHEN DECODING JSON \(error)")
        }
        
        guard let results = year else {
            print("YEAR IS NIL")
            return
        }
        
        print(results)   
    }
}

JSON

代码语言:javascript
复制
{
    "01/01/2020": {
   
        "MP" : {
            "M" : [
                {"title" : "m1", "description" : "1", "time" : "12:30pm"},
                {"title" : "m2", "description" : "2", "time" : "1:30pm"},
                {"title" : "m3", "description" : "3", "time" : "2:30pm"}
            ],
            "S" : [
                {"title" : "s1", "description" : "1", "time" : "1pm"}
            ]
        },
        "WP" : [
            { "title" : "abc", "values" :  [12, 10, 6]},
            { "title" : "def", "values" :  [8]}
        ]
    },
    "01/29/2020": {
        
        "MP" : {
            "M" : [{"title" : "m1", "description" : "1", "time" : "12:30pm"}],
            "S" : [{"title" : "s1", "description" : "1", "time" : "12:30pm"}]
        },
        "WP" :[{ "title" : "def", "values" :  [8]}]
    }
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-27 23:33:18

首先,用数组替换let WP: UserWP

代码语言:javascript
复制
struct UserDay: Codable {
    let MP: UserMP
    let WP: [WPData]
}

然后解码[String:UserDay]而不是UserDay

代码语言:javascript
复制
try JSONDecoder().decode([String:UserDay].self, from: Data(jsonString.utf8))

请注意,这将返回一个包含键值对"01/01/2020"UserDay object的dict。

这意味着不能将字典[String: UserDay]分配给UserDay变量。相反,您可以通过某个日期的键访问该数据集(例如。"01/01/2020"),然后将其赋值给您的UserDay变量:

代码语言:javascript
复制
let result = try JSONDecoder().decode([String:UserDay].self, from: Data(jsonString.utf8))
let someYear = result["01/01/2020"]

请注意,someYear将是可选的,因此您可能希望提供默认值或强制展开它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62616538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档