gpt4 book ai didi

json - 拿到Alamofire的返回数据后下一步该怎么办?

转载 作者:行者123 更新时间:2023-11-30 14:00:42 25 4
gpt4 key购买 nike

我使用 Alamofire 从网站请求数据,然后以这种方式打印结果:

if let resultData = response.result.value {
print(resultJson)
}

它在控制台中显示如下:

(
{
name = "Liu Bei";
strength = 4;
wisdom = 5;
},
{
name = "Guan Yu";
strength = 7;
wisdom = 5;
},
{
name = "Zhang Fei";
strength = 7;
wisdom = 3;
}
)

它的类型似乎是AnyObject,但是我可以解析它的什么类型,以便我可以获取每个字符的名称和强度等数据?

最佳答案

正如 vadian 在评论中已经指出的那样。

JSON 类型为 [[String:AnyObject]]

if let resultArray = jsonResult as? [[String:AnyObject]]{
for dictionary in resultArray{
print(dictionary['name'])
if let strength = dictionary['strength'] as? Int{
print(strength)
}
}
}

此代码将首先检查 JSON 是否确实是 [[String:AnyObject]] 类型。然后它将迭代数组并打印数组中的每个名称和强度。

关于json - 拿到Alamofire的返回数据后下一步该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com