我正在尝试遍历从POST到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 [&#34; ItemNumber&#34;],responseJSON [&#34; Count&#34;],responseJSON [&#34; Total&# 34;]和responseJSON [&#34; PercentComplete&#34;]
答案 0 :(得分:0)
问题在于您正在尝试将字典数组转换为字典。
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命名约定,这是变量的较低值。