type'[[String:AnyObject]]'没有下标成员

时间:2017-09-19 02:11:34

标签: json swift parsing

[let appUrl = URL(string: "some link")!

let data = try Data(contentsOf: appUrl)

let json2 = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String : AnyObject]

let post = json2["posts"] as? [[String:AnyObject]]

// here I want to connect to image but its still tell me the problem  
//type '[[String : AnyObject]]' has no subscript members              
let image = post["image"] as [[String:AnyObject]]
print(image)

image

1 个答案:

答案 0 :(得分:0)

这是因为你定义了

// Array of [[String:AnyObject]]
let post = json2["posts"] as? [[String:AnyObject]] 

你要尝试使用的是

let image = post["image"] as? [[String:AnyObject]] //Wrong

是否可以使用“image”字符串索引访问帖子数组?您必须将索引传递给post数组

 // index may be changed or you can iterate post array.
let image = post?[2]["image"] as? [[String:AnyObject]] //Right

希望你能了解你做了什么以及你必须做些什么。