SwiftyJSON object back to string

与世无争的帅哥 提交于 2019-11-30 05:42:53

From the README of SwiftyJSON on GitHub:

//convert the JSON to a raw String
if let string = libraryObject.rawString() {
//Do something you want
  print(string)
}

Swift 4.2 Version

//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'
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!