extension Data {
func printJson() {
do {
let object = try JSONSerialization.jsonObject(with: self)
let jsonData = try JSONSerialization.data(withJSONObject: object, options: .prettyPrinted)
let jsonString = String(data: jsonData, encoding: .utf8)
print(jsonString ?? "nil")
} catch {
print("Not valid json format \\(self)")
}
}
}
Data를 String으로 만들때 쓰는 init 2개
print(String(decoding: data, as: UTF8.self))
print(String(data: b!, encoding: .utf8))