[英]Json Tableview Swift
this post follows an old post where some people helped me for a similar problem. 这篇文章之前是一篇旧文章,其中有人帮助我解决了类似问题。
I currently developing an app which list user object by making an request to a webservice, which send a response in JSON in this format : 我目前正在开发一个应用,该应用通过向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"
}
}
I search a method to retrieve for each dictionary the value title and price and put them in a tableView. 我搜索一种方法来检索每个词典的值标题和价格,并将它们放在tableView中。
Code I used (tableviewcontroller) : 我使用的代码(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)
}
}
}
But it doesn't work, I put a breakpoint, and Xcode stop to interpret here : 但这不起作用,我放了一个断点,然后Xcode在这里停止解释:
for dict in jsonArray
Thanks for your help. 谢谢你的帮助。
This example JSON is not valid: it lacks a ]
before the last }
. 此示例JSON无效:它在last }
之前缺少]
。
But I guess this is just a pasting typo and the JSON you're using is properly formatted, so your problem is that you need to first access the objects
key of your dictionary. 但是我想这只是一个粘贴错误,并且您正在使用的JSON格式正确,因此您的问题是您需要首先访问字典的objects
键。
This key holds a value of an array of dictionaries, so we're using it as a typecast: 该键包含字典数组的值,因此我们将其用作类型转换:
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)
}
}
}
}
First we cast the result of NSJSONSerialization.JSONObjectWithData
as the dictionary: [String:AnyObject]
, then we access the value for the objects
key, then we cast this value as an array of dictionaries: [[String:AnyObject]]
. 首先,我们将NSJSONSerialization.JSONObjectWithData
的结果NSJSONSerialization.JSONObjectWithData
为字典: [String:AnyObject]
,然后访问objects
键的值,然后将该值转换为字典数组: [[String:AnyObject]]
。
Remember, with JSON format, dictionaries are formatted with {}
and arrays are formatted with []
. 请记住,使用JSON格式,字典用{}
格式化,而数组用[]
格式化。
Your example is {key:[{},{}]}
so it's a dictionary holding an array of dictionaries. 您的示例是{key:[{},{}]}
所以它是一本字典,包含一系列字典。
Update for Swift 2.0 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)
}
}
}
}
Try this : 尝试这个 :
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.