分享
三行代码  ›  专栏  ›  技术社区  ›  justADummy

Swift Json键未找到

  •  0
  • justADummy  · 技术社区  · 1 月前

    请帮助使这项工作我一直试图找出一个星期的json和swift,并面临这个问题至今5个小时。

    解码JSON keyNotFound时出错(CodingKeys(stringValue:“MP”,intValue:nil),Swift.DecodingError.Context(编码路径:[],调试描述:“没有与键编码键相关联的值(stringValue:”MP“,intValue:nil”(“MP”)。“underlinegror:nil))

    年为零

    代码

    
    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]
    }
    
    
    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)
            
        }
        
        
    }
    
    

    {
        "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]}]
        }
        
    }
    
    1 回复  |  直到 1 月前
        1
  •  1
  •   pawello2222    1 月前

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

    struct UserDay: Codable {
        let MP: UserMP
        let WP: [WPData]
    }
    

    [String:UserDay] 而不是 UserDay :

    try JSONDecoder().decode([String:UserDay].self, from: Data(jsonString.utf8))
    

    注意,这将返回一个带有键值对的dict "01/01/2020" : UserDay object

    [String: UserDay] 给一个 变量。相反,您可以通过某个日期(例如“01/01/2020”)的键访问dict,然后将其分配给 用户日

    let result = try JSONDecoder().decode([String:UserDay].self, from: Data(jsonString.utf8))
    let someYear = result["01/01/2020"]
    

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