【发布时间】:2015-08-16 11:26:46
【问题描述】:
我目前正在开发一个应用程序,它通过向 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"
},
}
我的网络服务为每个对象创建一个字典 (0;1;2;3....)
我搜索了一个方法来为每个字典检索值标题和价格并将它们放在一个 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)
我做错了什么?
谢谢。
【问题讨论】: