收到错误时,我正在从数据转换JSON:
“ NSInvalidArgumentException”,原因:“ *** + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON写入中的无效顶级类型”
数据按预期方式打印,但JSON没有打印。我认为代码在do
语句后的第一行中失败。
do {
var str: String = "http://localhost:5000/requirements/\(Array.joined(separator: "_"))"
let url = URL(string: str)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
guard let data = data else { return }
print(data)
do {
let json = try? JSONSerialization.data(withJSONObject: data)
print(json)
if let json = json {
print(json)
let decodedList = try? JSONDecoder().decode(List.self, from: json)
print(decodedList)
if let items = decodedList {
listToDisplay = items.itemsAsString.components(separatedBy: ",")
print(listToDisplay)
}
}
} catch let jsonErr {
print("Error serializing json", jsonErr)
}
}.resume()
return listToDisplay
}