반응형
문자열로 스위프티JSON 오브젝트 되돌리기
SwiftyJ를 사용하고 있습니다.SON 라이브러리: JSON을 스위프트오브젝트로 해석합니다.JSON 객체를 생성하여 읽고 쓸 수 있습니다.
// Create json object to represent library
var libraryObject = JSON(["name":"mylibrary","tasks":["Task1","Task2","Task3"]])
// Get
println(libraryObject["name"])
println(libraryObject["tasks"][0])
// Set
println("Setting first task to 'New Task'")
libraryObject["tasks"][0] = "New Task"
// Get
println(libraryObject["tasks"][0])
// Convert object to JSON and print
println(libraryObject)
이 모든 것이 예상대로 작동합니다.libraryObject를 JSON 형식의 문자열로 변환하고 싶을 뿐입니다.
println(libraryObject) 명령어를 실행하면 원하는 내용이 콘솔에 출력되지만 문자열로 가져올 방법을 찾을 수 없습니다.
라이브러리 오브젝트String value 및 library Object.문자열은 둘 다 빈 값을 반환하지만 println("content:+libraryObject")을 시도하면 문자열을 JSON에 추가하려고 하면 오류가 발생합니다.
SwiftyJ의 README에서GitHub의 SON:
//convert the JSON to a raw String
if let string = libraryObject.rawString() {
//Do something you want
print(string)
}
//convert the JSON to a raw String
if let strJson = jsonObject.rawString() {
// 'strJson' contains string version of 'jsonObject'
}
//convert the String back to JSON (used this way when used with Alamofire to prevent errors like Task .<1> HTTP load failed (error code: -1009 [1:50])
if let data = strJson.data(using: .utf8) {
if let jsonObject = try? JSON(data: data) {
// 'jsonObject' contains Json version of 'strJson'
}
}
언급URL : https://stackoverflow.com/questions/31142091/swiftyjson-object-back-to-string
반응형
'programing' 카테고리의 다른 글
JSON 형식의 POST 데이터 (0) | 2023.03.15 |
---|---|
Woocommerce에서 카트에 추가됨 메시지 숨기기 (0) | 2023.03.15 |
WordPress 테마에 jQuery를 포함하려면 어떻게 해야 합니까? (0) | 2023.03.15 |
스프링 - 로컬 파일 시스템에 저장하지 않고 대용량 멀티파트 파일 업로드를 데이터베이스로 스트리밍하는 방법 (0) | 2023.03.15 |
WordPress에서 자동으로 페이지 생성 (0) | 2023.03.15 |