【发布时间】:2017-02-24 13:39:42
【问题描述】:
我尝试通过键从数组字典中选择数据。
我可以做到索引号。
但这是我的字典 [[String : AnyObject]] 什么是 key 存储每个元素时i 从 json 反序列化到字典。
因为字典集 key value 对并且字典中的每个值都与一个 唯一键 相关联。
这是我的 Json:
{
"photos": {
"page": 1,
"pages": 1,
"perpage": 500,
"total": 36,
"photo": [
{
"id": "9671201784",
"owner": "34507951@N07",
"secret": "fb55a848bc",
"server": "5494",
"farm": 6,
"title": "Burning Man 2013",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"url_m": "https://farm6.staticflickr.com/5494/9671201784_fb55a848bc.jpg",
"height_m": "333",
"width_m": "500",
"is_primary": 1,
"has_comment": 0
},
{
"id": "16553346708",
"owner": "116399434@N04",
"secret": "092f304f0e",
"server": "7630",
"farm": 8,
"title": "Burning Man",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"url_m": "https://farm8.staticflickr.com/7630/16553346708_092f304f0e.jpg",
"height_m": "244",
"width_m": "500",
"is_primary": 0,
"has_comment": 0
}],
"stat": "ok"
}
反序列化快速代码;
if let photos = parsedResult[Constants.FlickerResponseKeys.Photos] as?[String : AnyObject ]{
// print(photos[Constants.FlickerResponseKeys.Photo])
if let photo = photos[Constants.FlickerResponseKeys.Photo] as? [[String : AnyObject]] {
print(photo[0])
}
}
【问题讨论】:
标签: json swift dictionary