作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试循环遍历从 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/
我是一名优秀的程序员,十分优秀!