如何在Swift iOS中使模型类遵循JSON响应

编程入门 行业动态 更新时间:2024-10-05 19:19:28
本文介绍了如何在Swift iOS中使模型类遵循JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我是swift ios的初学者,我的要求是必须显示对表列表的Json响应,我从网络服务获得了响应,并且响应如下所示

Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below

我的要求是如何将模型类映射到Array,以及如何在tableList中显示它们,请帮忙

MY requirement is how to map that model classes to Array and how to display them in tableList can some one help me please

[{ "_id" : "5470def9e0c0be27780121d7", "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png", "name" : "Mondo", "hasVip" : false, "location" : { "city" : "Madrid" } }, { "_id" : "540b2ff281b30f3504a1c72f", "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png", "name" : "Teatro Kapital", "hasVippler" : false, "location" : { "address" : "Atocha, 125", "city" : "Madrid" } }, { "_id" : "540cd44581b30f3504a1c73b", "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png", "name" : "Charada", "hasVippler" : false, "location" : { "address" : "La Bola, 13", "city" : "Madrid" } }]

映射: 俱乐部:-

class Club { var id: String = "" var name: String = "" var imageUrl: String = "" var hasVip: Bool = false var desc: String = "" var location: [Location] = [] }

位置:-

class Location { var country: String = "" var city: String = "" var address: String = "" var zip: String = "" var underground: [String] = [] }

NSURl会话代码:-

class BackGroundPostCall: NSObject { var delegate:PostProtocol? func callPostService(url:String,params:NSDictionary){ print("url is===>\(url)") let request = NSMutableURLRequest(URL: NSURL(string:url)!) let session = NSURLSession.sharedSession() request.HTTPMethod = "POST" //Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json. request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: []) let task = session.dataTaskWithRequest(request) { data, response, error in guard data != nil else { print("no data found: \(error)") return } do { if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray { print("Response: \(json)") } else { let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary print("Error could not parse JSON: \(jsonStr)") } } catch let parseError { print(parseError)// Log the error thrown by `JSONObjectWithData` let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) print("Error could not parse JSON: '\(jsonStr)'") } } task.resume() } }

推荐答案

您可以使用以下网址创建模型类: http: //www.jsoncafe/

You can make model class using this url : www.jsoncafe/

打开此链接,然后将json放在JSON选项卡中,然后选择所需的任何代码模板.并且如果需要,您还可以添加前缀类名和根类名,否则它是可选的.最后单击生成"按钮,您的json类就准备好了!!

open this link and put your json in JSON tab and select any Code Template that is you want. and there is you can also add prefix class name and root class name if you want other wise it is optional. and last click on generate button, your json class is ready.!!

更多推荐

如何在Swift iOS中使模型类遵循JSON响应

本文发布于:2023-11-26 23:13:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   如何在   iOS   Swift   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!