【发布时间】:2018-05-20 07:29:50
【问题描述】:
我不想获得名为symbolString 的字符串的值。我不知道该怎么做。这是我到目前为止得到的。我首先尝试进入 Data 子项,然后转到 symbolString 以获取 ImageUrl 值。它似乎不起作用。请帮忙!
final let urlString = "https://www.cryptocompare.com/api/data/coinlist/"
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
if let DataArray = jsonObj!.value(forKey: "Data") as? NSArray {
print(DataArray)
for Data in DataArray{
if let DataDict = Data as? NSDictionary {
if let nameURL = DataDict.value(forKey: "\(self.symbolString)") as? NSDictionary {
if let name = nameURL.value(forKey: "ImageUrl") {
self.imageString.append(name as! String)
print(self.imageString)
}
}
}
}
}
}
}).resume()
}
【问题讨论】:
-
JSON 中没有数组 (
[])。所有集合类型都是字典 ({})。顺便说一句,变量名应该以小写字母开头,不要在 Swift 中使用NSArray, NSDictionary和valueForKey -
您的数据中似乎没有任何
symbolString实例。您希望在哪里找到它? -
symbolString 是一个字符串,例如“BTC”。这行得通,我已经检查过了。但是,如果您查看网址,我首先需要输入“数据”,而不是例如“BTC”(symbolString)。但我不知道该怎么做。