我试图从嵌套字典数组中获取数据,这是JSON响应的结果。
func fetchData() {
let urlString = "https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion?api_key=\(self.apiKey)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print(error ?? "ERROR!")
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
print(parsedData)
print(parsedData["type"] ?? "")
print(parsedData["version"] ?? "")
print(type(of: parsedData))
print(type(of: parsedData["data"]!))
//ERROR HERE.
//"Cannot subscript a value of type '[String, Any]' with an index of type '(String, (String, Any).Type)'
let innerItem = parsedData["Aatrox", (String, Any)]
} catch let error as NSError {
print(error)
}
}
}.resume()
以下是印刷品:
["type": champion, "version": 7.4.3, "data": {
Aatrox = {
id = 266;
key = Aatrox;
name = Aatrox;
title = "the Darkin Blade";
};
Ahri = {
id = 103;
key = Ahri;
name = Ahri;
title = "the Nine-Tailed Fox";
};
Akali = {
id = 84;
key = Akali;
name = Akali;
title = "the Fist of Shadow";
};
Alistar = {
id = 12;
key = Alistar;
name = Alistar;
title = "the Minotaur";
};
}]
champion
7.4.3
Dictionary<String, Any>
__NSDictionaryI
我正在努力获得&#34; id&#34;为&#34; Aatrox&#34;。
我将如何做到这一点?
是因为parsedData的类型[&#34;数据&#34;]!是__NSDictionaryI ??
感谢您的帮助。
答案 0 :(得分:3)
首先,您需要提取data
词典,然后访问其他词典。
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
if let passDic = parsedData["data"] as? [String:Any],
let innerItem = passDic["Aatrox"] as? [String: Any] {
print(innerItem)
}
} catch {
print(error)
}
答案 1 :(得分:0)
似乎应该是let innerItem: [String: Any] = parsedData["Aatrox"]
答案 2 :(得分:0)
int seconds = 5;
final short[] data = new short[samples * seconds];
recorder.readSamples(data, 0, data.length);
这件事对我有用..