gpt4 book ai didi

ios - 如何在 Swift 3 中解析来自 URL 的 JSON 数据

转载 作者:行者123 更新时间:2023-11-30 12:13:32 24 4
gpt4 key购买 nike

我正在尝试循环遍历从 POSTing 到 URL 检索到的 JSON 字典。检索数据并将其序列化为 JSON 对象后,我无法访问数据的各个部分。

func retrieveLotStatus(lotNumber: String) {
let allowedChars = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#-[] ").inverted)

var url = URLRequest(url: URL(string: "fakeURLHere")!)
let BodyData = "lotNum=" + lotNumber + "&field=" + PHASE.addingPercentEncoding(withAllowedCharacters: allowedChars)!
url.httpMethod = "POST"
url.httpBody = BodyData.data(using: .utf8)
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No Data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
print(responseJSON)
if let jsonData = responseJSON as? [String: Any] {
if let ItemNumber = jsonData["ItemNumber"] as? [String: Any] {
print(ItemNumber)
}
}
}
task.resume()
}

这是打印响应以供引用时检索 JSON 数据的示例

    Optional(<__NSArrayI 0x17d08c90>(
{
Count = 4;
ItemNumber = 1;
PercentComplete = "100.00";
Total = 4;
},
{
Count = 1;
ItemNumber = 10;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 10a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 11;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 11a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 4;
ItemNumber = 1a;
PercentComplete = "100.00";
Total = 4;
},
{
Count = 2;
ItemNumber = 2;
PercentComplete = "100.00";
Total = 2;
},
{
Count = 2;
ItemNumber = 2a;
PercentComplete = "100.00";
Total = 2;
},
{
Count = 1;
ItemNumber = 3;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 3a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 4;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 4a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 5;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 5a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 6;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 6a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 7;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 7a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 8;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 8a;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 9;
PercentComplete = "100.00";
Total = 1;
},
{
Count = 1;
ItemNumber = 9a;
PercentComplete = "100.00";
Total = 1;
}
)
)

我试图循环遍历每个对象并将值附加到表中。然而,在我将它们格式化为 UITableCell 之前,我尝试简单地读取每个 responseJSON["ItemNumber"]、responseJSON["Count"]、responseJSON["Total"] 和 responseJSON["PercentComplete"]

最佳答案

问题是您正在尝试将字典数组转换为字典。

if let jsonData = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [[String:Any]] {
for item in jsonData {
if let itemNumber = item["ItemNumber"] as? Int, let count = item["Count"] as? Int, let percentComplete = item["PercentComplete"] as? Double, let total = item["Total"] as? Int {
//use data
}
}
}

还要确保遵循 Swift 命名约定,即变量采用小驼峰命名。

关于ios - 如何在 Swift 3 中解析来自 URL 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720359/

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