개발/iOS
Api에서 받아온 data를 예쁘게 출력하기
빙수킹
2021. 3. 20. 21:16
데이터를 받아와서 테스트로 콘솔에 출력해볼 때, 백슬래시 범벅의 가독성 나쁜 상태와 마주한다..
아래와 같은 코드로 찍어볼 수 있지만..
굳이 2번 변환해야되는데 이거 말고 좋은건 없을까?..
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:AnyObject]
let prettyJson = try JSONSerialization.data(withJSONObject: json, options:JSONSerialization.WritingOptions.prettyPrinted )
if let prettyString = String(data: prettyJson, encoding: String.Encoding.utf8) {
print(prettyString)
}
} catch { print("Failed to load: \(error.localizedDescription)") }
여기 나와 같은 고민 .. 근데 뭔가 게시물을 여기저기 찾아봐도 더 나은걸 찾지 못했다.. 아시는분 있으면 알려주세요우
Is there a better way to "Pretty Print" the json string than I'm using
I have json data comes server side. In case I use the next code I get not pretty printed one line string: print(String(bytes: jsonData, encoding: String.Encoding.utf8)) To make it pretty printed...
stackoverflow.com