json - TableView Json swift

标签 json swift tableview

我目前正在开发一个应用程序,它通过向网络服务发出请求来列出用户对象,该网络服务以这种格式发送 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))
                }

存储库 Controller :

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)

我做错了什么?

谢谢。

最佳答案

以下是获取 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)
            }
        }
    }
}

如果你愿意,这也可以用来初始化你的 Repository 类:

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 结果的类型转换以反射(reflect)这一点:[[字符串: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)
        }
    }
}

关于json - TableView Json swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034544/

相关文章:

javascript - 如何在jquery中解析这个json?

iOS:UIView 外的实心边框

ios - 如何访问类实例数组中的数组?

swift - 在 UITableView 中显示来自结构的数据

json - Instagram:是否可以使用#tag 获取用户名、关注者编号和搜索照片?

ios - 将 swift 数组解析为有效的 json

arrays - 附加到数组类属性时的 EXC_BAD_ACCESS

swift - 从 Swift 中的触摸返回坐标

objective-c - 按下按钮时如何更改 TableView 中的数据

java.lang.NoClassDefFoundError : com/google/api/client/json/JsonFactory