Json 类型';[[String:AnyObject]';没有下标成员
发生这种情况是因为您定义了Json 类型';[[String:AnyObject]';没有下标成员,json,swift,parsing,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:Any
[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)
你想用的是
// Array of [[String:AnyObject]]
let post = json2["posts"] as? [[String:AnyObject]]
可以使用“image”字符串索引访问post数组吗?您必须将索引传递给post
数组
let image = post["image"] as? [[String:AnyObject]] //Wrong
希望您能了解您已经做了什么以及必须做什么。您的JSON的实际结构是什么?您已将
post
声明为字典数组,但在下一行中尝试将post
视为字典。如何操作?您可以发布JSON的一些示例结构吗?func fetchArticles(){let urlRequest=urlRequest(url:url(string:)let task=URLSession.shared.dataTask(with:urlRequest){(数据、响应、错误)在if error!=nil{print(error as Any)return请将此添加到th Q,而不是在评论中。没有人可以阅读此内容。谢谢!这对我有帮助,但还有一个问题我不仅有更多索引[2]我能写什么?让image=post?[2][“image”]作为?[[String:AnyObject]]//我必须更改[2]什么才能显示我的所有数组?谢谢!@MahmudHacialiyev如果你提供json结构,我会给你一些建议。
// index may be changed or you can iterate post array.
let image = post?[2]["image"] as? [[String:AnyObject]] //Right