我有一个充满字典的数组,
(
{
"id_color" = 8;
nombre = "Amarillo p\U00e1lido";
},
{
"id_color" = 9;
nombre = "Amarillo p\U00e1lido/toffy claro";
},
{
"id_color" = 13;
nombre = Azul;
},
{
"id_color" = 12;
nombre = Magenta;
},
{
"id_color" = 18;
nombre = Morado;
})
我可以访问字典的数据来执行此操作:
let coloresArray = jsonResult["colores"] as! NSArray
let colorDict = coloresArray[0] as! NSDictionary
print (colorDict["nombre"]!)
但是有没有办法访问键的值做这样的事情
jsonResult["colores"][0]["nombre"]
感谢您的回答
最佳答案
您需要正确地施放步骤。解析 JSON 数据时避免强制转换和强制展开。
获取所需数据的正确且安全的方法是:
if let colores = jsonResult["colores"] as? [[String:Any]], let nombre = colores[0]["nombre"] as? String {
print(nombre)
}
关于arrays - 从 JSON 解析数组内的字典获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49240549/