gpt4 book ai didi

json - 正确访问json文件

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:23 24 4
gpt4 key购买 nike

我正在访问 API 以检索播放器的数据。

当我使用 AlamoFire 发送 GET 请求时,我得到以下 JSON[summonerName.lowercaseString]! 的结果:

{
id = 11111111;
name = example;
profileIconId = 1;
revisionDate = 1111971248000;
summonerLevel = 30;
}

我只想检索 id 值。

我认为这是一本字典,所以我做了 JSON[summonerName.lowercaseString]!["id"] 但它提示我不能下标 AnyObject。我怎样才能访问它?

最佳答案

因为编译器不知道 JSON[summonerName.lowercaseString] 的类型,所以不能将其用作字典。你知道它是一本字典,但编译器不知道。

帮助指定类型:

if let object = JSON[summonerName.lowercaseString] as? [String:AnyObject] {
if let id = object["id"] as? Int {
// use id
}
if let name = object["name"] as? String {
// use name
}
}

等等

关于json - 正确访问json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826470/

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