gpt4 book ai didi

json - Tinder Like 检索卡中的 Web 服务数据

转载 作者:行者123 更新时间:2023-11-30 14:18:46 24 4
gpt4 key购买 nike

我目前正在开发一款类似 Tinder 的应用程序,它可以从网络服务中检索一些数据。

我已经编写了我的函数来检索数据,这就像一个魅力,并且我还实现了卡片模型。

问题是,我不知道如何编码事实以将解码 json 数据放入我的模型中。

我的代码:

//我将数据发送到我的 werbservice 以检索数据

var post:NSString = "recherche=\(recherche)&category=\(category)&zipCode=\(zipCode)&price=\(price)"

var url:NSURL = NSURL(string:"urlofmywebservice")!

来自网络服务的响应:

[
{
"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 解码:https://github.com/Alamofire/Alamofire + https://github.com/SwiftyJSON/SwiftyJSON

它们确实非常用户友好。

然后你可以做这样的事情:

    Alamofire.request(.GET, url).responseJSON {
(_, _, data, _) in
var json = JSON(data!)
var text = json["text"].string!
}

关于json - Tinder Like 检索卡中的 Web 服务数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com