草庐IT

Decodable

全部标签

json - Swift 4 Decodable - 没有与键 CodingKeys 关联的值

这个问题在这里已经有了答案:HowdoIusecustomkeyswithSwift4'sDecodableprotocol?(4个答案)关闭3年前。我正在我的Swift应用程序中解码一个JSON响应,并且代码曾经工作直到它决定停止工作。这是我的json响应{"foods":[{"food_name":"MilkChocolate","brand_name":"Snickers","serving_weight_grams":41.7,"nf_calories":212.3,"nf_total_fat":11.6,"nf_saturated_fat":4,"nf_total_carbo

ios - 在 Decodable 解码函数中检索 JSON 字符串

在解码符合Decodable的ClassA时,我想检索其中一个属性的值以进行自定义解码。我该如何实现?classClassA:Decodable{lettitle:String?letdata:MyCustomNotDecodableNSObject?privateenumCodingKeys:String,CodingKey{casetitlecasedata}requiredinit(fromdecoder:Decoder)throws{letcontainer=trydecoder.container(keyedBy:CodingKeys.self)title=try?conta

ios - 使用 Decodable 链接多个 JSON 请求 - Swift 5

我的“url”对象有一个链接,该链接捕获用户在搜索栏中键入的内容以完成链接,然后开始JSON过程。在JSON完成解析后,第一个链接以另一个链接响应。在我的ifletvalidLink=result.link中,您会看到我将链接信息存储到一个数组中。现在我不确定我是否应该在我的ifletvalidLink=result中开始另一个JSON响应,或者我是否应该像我在下面的代码中尝试做的那样创建一个新函数并基本上复制和在下面粘贴相同的JSON信息以重新解析它。第二个链接出现解析错误。最有效和正确的方法是什么?我真的被困在这里了。我尝试创建另一个函数,该函数使用来自第一个JSON解析的信息使用

ios - 如何在 Swift 4 中将 Encodable 或 Decodable 作为参数传递?

我正在学习JSONParsing。我按照教程进行操作,得到的是:guardleturl=URL(string:"http://localhost/test-api/public/api/register")else{return}varrequest=URLRequest(url:url)request.httpMethod="POST"letnewUser=User.init(name:self.collectionTF[0].text,email:self.collectionTF[1].text,password:self.collectionTF[2].text)do{letj

ios - 如何在 Swift 中使用 Decodable?

我在我的项目中使用免费日期API。我正在使用Decodable来解析JSON数据。我在这里创建了我的结构:-structjsonStruct:Decodable{varmessage:Bool?vardata:[dateData]}structdateData:Decodable{varquarter:Int?varday:String?varmonth:String?}这是我使用解码器的代码:-letjsonUrlString="https://api.lrs.org/random-date-generator?lim_quarters=40&source=api-docs"guar

json - 如何使用 Decodable 协议(protocol)将此 JSON 转换为 Swift 结构?

注意:这个问题我已经看过了->HowdoIusecustomkeyswithSwift4'sDecodableprotocol?但它没有解释如何编码/解码枚举这是我想要的结构:structMyStruct:Decodable{letcount:PostType}enumPostType:Decodable{casefast(value:Int,value2:Int)caseslow(string:String,string2:String)}现在我知道我希望我的结构看起来如何,问题是:我不知道init函数在PostType枚举中应该是什么样子。我使用下面的代码来帮助我快速构建JSON。

swift - 字符串字典 :Any does not conform to protocol 'Decodable'

这个问题在这里已经有了答案:Fatalerror:DictionarydoesnotconformtoDecodablebecauseAnydoesnotconformtoDecodable(2个答案)关闭4年前。我正在尝试实现一个Decodable来解析json请求,但json请求在对象内部有一个字典。这是我的代码:structmyStruct:Decodable{letcontent:[String:Any]}enumCodingKeys:String,CodingKey{casecontent="content"}但是我收到了这个错误:类型“MyClass.myStruct”不符

json - 使用 Swift 的 Decodable 协议(protocol)解析 CoinMarketCap API

我已阅读HowtodecodeanestedJSONstructwithSwiftDecodableprotocol?它没有解决我将字符串文字数字值用作根字典的特定用例。还有HowtodecodeanestedJSONstructwithSwiftDecodableprotocol?ImanouPetit的回答。Can'tdecodeJSONdatafromAPILeoDabus的回答。货币本身就是字典,由data字典中的文字字符串数字表示,所以这让我很反感。我正在寻找使用枚举的最多的Swifty4模型,在其中可以很容易地看到哪些容器对应于哪些词典。附注DavidBerry给出了一个很

swift - 使用 Decodable 将空 JSON 字符串值解码为 nil

假设我有这样一个结构:structResult:Decodable{letanimal:Animal?}像这样的枚举:enumAnimal:String,Decodable{casecat="cat"casedog="dog"}但是返回的JSON是这样的:{"animal":""}如果我尝试使用JSONDecoder将其解码为Result结构,我会得到CannotinitializeAnimalfrominvalidStringvalue作为一个错误信息。我如何正确地将此JSON结果解码为动物属性为nil的Result? 最佳答案

swift - 使用 Swift 4 的 Decodable 协议(protocol)进行嵌套的 JSON 服务器响应

我正在尝试实现Swift4的新Decodable协议(protocol),但遇到了一些困难。这是我的JSON服务器响应:{"success":true,"errorCode":0,"message":"Succcess","data":{"name":"LoganHowlett","nickname":"TheWolverine","image":"http://heroapps.co.il/employee-tests/ios/logan.jpg","dateOfBirth":1880,"powers":["AdamantiumBones","Self-Healing","Adama