Json Tableview 斯威夫特

Posted

技术标签:

【中文标题】Json Tableview 斯威夫特【英文标题】:Json Tableview Swift 【发布时间】:2015-08-18 11:48:40 【问题描述】:

这篇文章是在一篇旧文章之后,有人帮助我解决了类似的问题。

我目前正在开发一个应用程序,它通过向 web 服务发出请求来列出用户对象,它以这种格式以 JSON 格式发送响应:


"objects": [
    
        "id": "28",
        "title": "test",
        "price": "56 €",
        "description": "kiki",
        "addedDate": "11-07-2015",
        "user_id": "1",
        "user_name": "CANOVAS",
        "user_zipCode": "69330",
        "category_id": "1",
        "category_label": "VEHICULES",
        "subcategory_id": "1",
        "subcategory_label": "Voitures",
        "picture": "",
        "bdd": ,
        "picture_url": "http://jdl-barreme-orange.dyndns.org/WEBSERVICE/pictures/test.JPG"
    ,
    
        "id": "27",
        "title": "ferrari",
        "price": "55 €",
        "description": "rouge jantes",
        "addedDate": "11-07-2015",
        "user_id": "1",
        "user_name": "CANOVAS",
        "user_zipCode": "69330",
        "category_id": "1",
        "category_label": "VEHICULES",
        "subcategory_id": "1",
        "subcategory_label": "Voitures",
        "picture": "",
        "bdd": ,
        "picture_url": "http://jdl-barreme-orange.dyndns.org/WEBSERVICE/pictures/ferrari.JPG"
    

我搜索了一个方法来为每个字典检索值标题和价格并将它们放在一个 tableView 中。

我使用的代码(tableviewcontroller):

if let jsonArray = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [[String:AnyObject]] 
for dict in jsonArray 
    if let title = dict["title"] as? String 
        println(title)
    


但是不行,我放了一个断点,Xcode在这里停下来解释:

for dict in jsonArray

感谢您的帮助。

【问题讨论】:

【参考方案1】:

此示例 JSON 无效:它在最后一个 之前缺少 ]

但我想这只是一个粘贴错字,并且您使用的 JSON 格式正确,所以您的问题是您需要先访问字典的 objects 键。

此键保存字典数组的值,因此我们将其用作类型转换:

if let json = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [String:AnyObject] 
    if let objects = json["objects"] as? [[String:AnyObject]] 
        for dict in objects 
            if let title = dict["title"] as? String 
                println(title)
            
        
    

首先我们将NSJSONSerialization.JSONObjectWithData 的结果转换为字典:[String:AnyObject],然后我们访问objects 键的值,然后我们将该值转换为字典数组:[[String:AnyObject]]

请记住,对于 JSON 格式,字典的格式为 ,数组的格式为 []

您的示例是 key:[,],因此它是一个包含字典数组的字典。


Swift 2.0 更新

if let json = try? NSJSONSerialization.JSONObjectWithData(urlData!, options: []) as? [String:AnyObject] 
    if let objects = json?["objects"] as? [[String:AnyObject]] 
        for dict in objects 
            if let title = dict["title"] as? String 
                print(title)
            
        
    

【讨论】:

当然是粘贴错误。再次感谢您,它现在可以工作了。您的解释非常清楚,我认为现在可以单独解决其中的一些问题。 :)【参考方案2】:

试试这个:

var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
   var DataFromJSon = jsonResult["objects"] as! NSArray
        for one in DataFromJSon 
          var title = one["title"] as! String 
           println(title)

       

【讨论】:

欢迎您...您只需要注意那些 [ 以便您知道是否需要更进一步提取数据。

以上是关于Json Tableview 斯威夫特的主要内容,如果未能解决你的问题,请参考以下文章

如何将按钮中的 panGesture 传递给 tableView? (斯威夫特 4)

tableView Cell 不显示数据,这是啥问题? (斯威夫特 3)

在终端上获取 URL 后如何将数据传递给 tableview?斯威夫特 5

斯威夫特 Xcode 11.4。将返回按钮添加到 TableView 导航项

传递参数时无法识别的选择器 - TableView 单元格中的 UIButton |斯威夫特 iOS

在 tableView:cellForRowAtIndexPath 中确定 iPhone 和 iPad 的不同单元格: