我有一個JSON
,它可能包含某些鍵值的null
值。如何處理從JSON返回的空值
這是我的代碼:
static func parseJSONString(jsonString: String?, key: String?) -> () -> String{
var jsonSubString: String?
func parseString() -> String{
if let data = jsonString?.data(using: .utf8){
if let content = try? JSONSerialization.jsonObject(with: data, options: []),
let array = content as? [[String: AnyObject]]
{
for jsondict in array {
if let jsondictOptional = jsondict[key!]{
jsonSubString = (jsondictOptional as? String)
}
else {
jsonSubString = " "
}
}
}
}
return jsonSubString!
}
return parseString
}
當我運行的應用程序,它崩潰。如何成功運行此代碼,以便我甚至可以處理從JSON返回的nulls
?