在 swift 中写入文件 3 将数据从 json 转换为 xml

write to file in swift 3 transforming the data from json to xml

请注意下面的猫是从服务器返回的 json 类型,当我使用 try! cats?.write(toFile: file, atomically: false) 该文件包含一个 xml 输出,而我需要保持 json,非常感谢任何帮助!

let text = ""
try! text.write(toFile: file, atomically: false, encoding: String.Encoding.utf8)

let parameters : NSMutableDictionary? = [`enter code here`]

let manager = AFHTTPSessionManager()

manager.post("http://appsandgamesinc.com/api/AppAsUser/getMedias/", parameters: parameters, progress: nil, success: { (task: URLSessionTask, responseObject) in
      let response = responseObject as? NSDictionary
      let medias = response?.object(forKey: "medias") as? NSDictionary

      let cats = medias as? NSDictionary
      print("cats:\(cats)")
NSKeyedArchiver.archivedData(withRootObject: medias)

      try! cats?.write(toFile: file, atomically: false)

      print("file://"+file)
})

猫的类型是NSDictionary,如果你想在文件中保存JSON然后使用数据保存它。 Swift 使用 Swift 的原生字典和 Array 而不是 NSDictionaryNSArray.

manager.post("http://appsandgamesinc.com/api/AppAsUser/getMedias/", parameters: parameters, progress: nil, success: { (task: URLSessionTask, responseObject) in
    if let response = responseObject as? [String:Any],
        let medias = response["medias"] as? [String:Any] {

        if let data = try? JSONSerialization.data(withJSONObject: medias) {
            try? data.write(to: URL(fileURLWithPath: file), options: .atomic)
        }
        print("cats:\(cats)")
        NSKeyedArchiver.archivedData(withRootObject: medias)                      
        print("file://"+file)
    }
})