表格视图 Json Swift

TableView Json Swift

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

{
 "0":
 {
  "id":"30",
  "title":"galaxys6",
  "price":"550",
  "description":"neuf",
  "addedDate":"2015-07-16 15:04:24",
  "user_id":"2",
  "user_name":"",
  "user_zipCode":"69003",
  "category_id":"1",
  "category_label":"PHONE",
  "subcategory_id":"1",
  "subcategory_label":"Phone",
  "picture":"",
  "bdd":{},
  "picture_url":"http:\/\/jdl-barreme-orange.dyndns.org\/WEBSERVICE\/pictures\/galaxy s6.JPG"
 },
 "1":
 {
  "id":"31",
  "title":"iphone4",
  "price":"570",
  "description":"neuf",
  "addedDate":"2015-07-16 15:14:54",
  "user_id":"2",
  "user_name":"",
  "user_zipCode":"69003",
  "category_id":"1",
  "category_label":"PHONE",
  "subcategory_id":"1",
  "subcategory_label":"Phone",
  "picture":"",
  "bdd":{},
  "picture_url":"http:\/\/jdl-barreme-orange.dyndns.org\/WEBSERVICE\/pictures\/iphone.JPG"
 },
}

我的网络服务为每个对象创建一个字典 (0;1;2;3....)

我搜索了一种方法来为每个字典检索值 title 和 price 并将它们放在 tableView 中。

我使用的代码(tableviewcontroller):

if let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary{

            // 4
             if let resp = jsonData["1"] as? [NSDictionary] {
                NSLog("%@", resp)
                // 5
                for item in resp {
                    repositories.append(Repository(jsonData: item))
                }

存储库控制器:

class Repository {

var name: String?
var description: String?
var html_url: String?

init(jsonData: NSDictionary) {
    self.name = jsonData["id"] as? String
    self.description = jsonData["description"] as? String
    self.html_url = jsonData["title"] as? String
}
}

但是不行,我打了个断点,xcode这里就停止解释了:

if let resp = jsonData["1"] as? [NSDictionary] {
                NSLog("%@", resp)

我做错了什么?

谢谢。

我猜是 [NSDictionary]

if let resp = jsonData["1"] as? [NSDictionary]

[NSDictionary] 是与 Array<NSDictionary> 相同的 NSDictionary 数组 只需删除括号 [] 并更改为

if let resp = jsonData["1"] as? NSDictionary

你在这里犯了一个错误

if let resp = jsonData["1"] as? [NSDictionary]

这应该是 NSDictionary 而不是 [NSDictionary](这将是一个字典数组)。

还有这个条件块

if let reposArray = jsonData["items"] as? [NSDictionary] 

永远不会被执行,因为 jsonData 不包含键 "items"。

以下是获取 JSON 的标题和价格的方法:

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

这也可用于初始化您的存储库 类 如果您需要:

class Repository {

    var name: String?
    var description: String?
    var html_url: String?

    init(jsonData: [String:AnyObject]) {
        self.name = jsonData["id"] as? String
        self.description = jsonData["description"] as? String
        self.html_url = jsonData["title"] as? String
    }
}

var repos = [Repository]()

if let json = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: nil) as? [String:AnyObject] {
    for (_, value) in json {
        if let dict = value as? [String:AnyObject] {
            let repo = Repository(jsonData: dict)
            repos.append(repo)
        }
    }
}

for repo in repos {
    println(repo.name)
    println(repo.description)
    println(repo.html_url)
}

在循环中我忽略了密钥:for (_, value) in json 但如果需要当然可以使用它:

for (key, value) in json {
    println(key)  // "1", "2", ...
    // ...
} 

更新:

根据您的评论询问如果您的数据格式不同如何使用此答案:如果您想要一个字典数组,请更改 NSJSONSerialization 结果的类型转换以反映这一点:[[String:AnyObject]]。接下来,您可以遍历数组以获取每个字典属性:

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)
        }
    }
}