我不想获得名为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()
}
答案 0 :(得分:0)
您可以尝试使用以下代码。它为我工作。
func downloadJsonWithURL() {
let url = "https://www.cryptocompare.com/api/data/coinlist/"
var urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let unwrappedDAta = data else {
print("Error unwrapping data")
return
}
do {
let responseJSON = try JSONSerialization.jsonObject(with: unwrappedDAta, options: []) as? NSDictionary
if let dataDict = responseJSON!["Data"] as? NSDictionary {
for (key, _) in dataDict {
if let keyDict = dataDict[key] as? NSDictionary {
if let imageURL = keyDict["ImageUrl"] as? String {
self.imageString.append(imageURL)
}
print(self.imageString)
}
}
}
} catch {
print("Could not get API data. \(error))")
}
}
task.resume()
}