TableView Json Swift

TableView Json Swift,json,swift,tableview,Json,Swift,Tableview,我目前正在开发一个应用程序,通过向Web服务发出请求来列出用户对象,Web服务以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_i

我目前正在开发一个应用程序,通过向Web服务发出请求来列出用户对象,Web服务以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"
 },
}
我的Web服务为每个对象创建一个字典(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]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)
}
在循环中,我忽略了json中(u,value)的键:
,但如果需要,当然可以使用它:

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

我已经尝试使用NSDictionary而不是[NSDictionary],但是我得到了:“无法使用类型为“(jsonData:(key:AnyObject,value:AnyObject))”的参数列表调用类型为“Repository”的初始值设定项。”。当然你是对的,这是一个错误,“如果让reposArray=jsonData[“items”]as?[NSDictionary]”从另一个控制器提供,很抱歉。很好,我们正在接近这个问题。首先投票表决答案。我已经尝试使用NSDictionary而不是[NSDictionary],但我得到了:“无法使用类型为“(jsonData:(key:AnyObject,value:AnyObject))”的参数列表调用类型为“Repository”的初始值设定项”。最后一个问题,如果我的JSON:{“objects”:[{“id”:“28”,“title:”test“,”price“:”56…而不是“{”0“,”id:”30“,”title:”galaxys6“,”price“:”550“…?感谢您的解释,它更容易理解,但是如果我在“if let jsonArray=nsjsjsonserialization.JSONObjectWithData(urlData!),选项:nil,错误:nil)上设置一个断点:“if let jsonArray=NSJSONSerialization.JSONObjectWithData(urlData!,选项:nil,错误:nil)为?[[String:AnyObject]]”,xcode不执行下一个代码“jsonArray中的dict”再次感谢您与我在一起的时间!用测试您的实际JSON是否有效。我已经用您在评论中给出的示例检查了我的更新,它工作正常。如果仍然存在问题,那么也许是时候创建一个全新的问题,详细说明这个特定的新错误了。:)是的,您是对的,关于信息,我的JSON是有效的,我只是认为我必须修改我的代码,以便在对象字段中输入,然后检索每个对象的值。谢谢!