使用三方库YYModel
pod 'YYModel'
转换
let resp = Response()
// 转Dictionary
let respDic = resp.yy_modelToJSONObject()
// 转JsonString
let respJsonString = resp.yy_modelToJSONString()
// 打印Json
do {
// 将Swift的Dictionary对象转换成Data类型
let jsonData = try JSONSerialization.data(withJSONObject: resp.yy_modelToJSONObject() as Any, options: .prettyPrinted)
// 将Data对象转换成String类型
if let jsonString = String(data: jsonData, encoding: .utf8) {
print(jsonString)
}
} catch {
print("Error converting to JSON: \(error)")
}