Json Tinderlike检索卡中的Web服务数据
我目前正在开发一个类似tinder的应用程序,它可以从Web服务中检索一些数据 我已经编写了检索数据的函数,该函数的工作方式很有魅力,我还实现了cardmodel 问题是,我不知道如何编写代码将解码的json数据放入模型中 我的代码: //我向werbservice发送的数据用于检索数据Json Tinderlike检索卡中的Web服务数据,json,web-services,swift,tinder,Json,Web Services,Swift,Tinder,我目前正在开发一个类似tinder的应用程序,它可以从Web服务中检索一些数据 我已经编写了检索数据的函数,该函数的工作方式很有魅力,我还实现了cardmodel 问题是,我不知道如何编写代码将解码的json数据放入模型中 我的代码: //我向werbservice发送的数据用于检索数据 var post:NSString = "recherche=\(recherche)&category=\(category)&zipCode=\(zipCode)&price=\(p
var post:NSString = "recherche=\(recherche)&category=\(category)&zipCode=\(zipCode)&price=\(price)"
var url:NSURL = NSURL(string:"urlofmywebservice")!
来自Web服务的响应:
[
{
"id": "1",
"title": "bmw",
"price": "500.00",
"description": "330",
"addedDate": "2015-05-18 00:00:00",
"user_id": "1",
"user_name": "CANOVAS",
"user_zipCode": "32767",
"category_id": "1",
"category_label": "VEHICULES",
"subcategory_id": "2",
"subcategory_label": "Motos",
"pictures":"http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_BMW_Z4_sDrive28i_3790993.jpg"
"bdd": {}
}
]
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary
let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
//[jsonData[@"success"] integerValue];
NSLog("Success: %ld", success);
if(success == 1)
{
let newCard = Model()
newCard.image = UIImage(data: pictures)
newCard.content = jsonData.valueForKey("content") as! NSstring
newCard.desc = jsonData.valueForKey("desc") as! NSstring
self.data.append(newCard)
NSLog("fetch new data")
如果我有一些数据:
[
{
"id": "1",
"title": "bmw",
"price": "500.00",
"description": "330",
"addedDate": "2015-05-18 00:00:00",
"user_id": "1",
"user_name": "CANOVAS",
"user_zipCode": "32767",
"category_id": "1",
"category_label": "VEHICULES",
"subcategory_id": "2",
"subcategory_label": "Motos",
"pictures":"http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_BMW_Z4_sDrive28i_3790993.jpg"
"bdd": {}
}
]
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary
let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
//[jsonData[@"success"] integerValue];
NSLog("Success: %ld", success);
if(success == 1)
{
let newCard = Model()
newCard.image = UIImage(data: pictures)
newCard.content = jsonData.valueForKey("content") as! NSstring
newCard.desc = jsonData.valueForKey("desc") as! NSstring
self.data.append(newCard)
NSLog("fetch new data")
我调用我的卡片,我想在其中放入来自我的Web服务的解码数据:
[
{
"id": "1",
"title": "bmw",
"price": "500.00",
"description": "330",
"addedDate": "2015-05-18 00:00:00",
"user_id": "1",
"user_name": "CANOVAS",
"user_zipCode": "32767",
"category_id": "1",
"category_label": "VEHICULES",
"subcategory_id": "2",
"subcategory_label": "Motos",
"pictures":"http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_BMW_Z4_sDrive28i_3790993.jpg"
"bdd": {}
}
]
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary
let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
//[jsonData[@"success"] integerValue];
NSLog("Success: %ld", success);
if(success == 1)
{
let newCard = Model()
newCard.image = UIImage(data: pictures)
newCard.content = jsonData.valueForKey("content") as! NSstring
newCard.desc = jsonData.valueForKey("desc") as! NSstring
self.data.append(newCard)
NSLog("fetch new data")
我已经尝试过一些东西,比如“jsonData.valueForKey(“desc”)as!NSstring”,但我得到了突破点
您有什么想法吗?我建议您使用SwiftyJSON和Alamofire进行HTML请求和JSON解码:+ 它们真的很方便用户 然后,您可以执行以下操作:
Alamofire.request(.GET, url).responseJSON {
(_, _, data, _) in
var json = JSON(data!)
var text = json["text"].string!
}
我总是这样做,也许我没有好的技术,你有我能做什么的例子吗?什么都没有,应用程序崩溃并停止:“newCard.content=jsonData[“message”]!.string”