【发布时间】:2018-06-18 07:38:59
【问题描述】:
我正在用 alamofire 快速解析 JSON。我正在尝试查看循环我的 json,但我的代码有问题。使用调试器查看代码时,我的应用程序不会进入 if 和 for。我的代码有什么问题? 我必须用“for”循环我的 json 并解析我的 json 数据。
Alamofire.request(apiToContact, method: .get, encoding: URLEncoding.default, headers: headersq).responseJSON { (response) in
print(response)
if response.result.isSuccess {
guard let resJson = response.result.value else { return }
print(resJson);
if let json = response.result.value as? [String:AnyObject] {
for entry in json {
print("\(entry)") // this is where everything crashes
}
}
if let JSON = response.result.value as? NSDictionary{
for entry in JSON {
print("\(entry)") // this is where everything crashes
}
}
}
if response.result.isFailure {
}
}
Print(response) 给出这个 json.:
SUCCESS: (
{
KoorX = "38.414745";
KoorY = "27.183055";
Yon = 1;
},
{
KoorX = "38.41474";
KoorY = "27.18382667";
Yon = 1;
},
{
KoorX = "38.422255";
KoorY = "27.15055167";
Yon = 1;
}
)
【问题讨论】:
-
"SUCCESS:" 这不是来自您的 JSON 吗?那么你的 JSON 是一个字典数组。类似:
[[String: Any]]。另外,如果崩溃,请阅读控制台中的错误消息,如果您不明白,请提供给我们。