在swift 3中写入文件,将数据从json转换为xml
请注意,下面的cats是从服务器返回的json类型,当我使用在swift 3中写入文件,将数据从json转换为xml,json,xml,swift3,writetofile,Json,Xml,Swift3,Writetofile,请注意,下面的cats是从服务器返回的json类型,当我使用时,请尝试!cats?.write(toFile:file,原子性:false) 该文件包含一个xml输出,而我需要保留为json,非常感谢任何帮助 let text = "" try! text.write(toFile: file, atomically: false, encoding: String.Encoding.utf8) let parameters : NSMutableDictionary? = [`enter c
时,请尝试!cats?.write(toFile:file,原子性: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)
})
cats的类型是
NSDictionary
,如果要将JSON保存在文件中,请使用数据保存它。同样对于Swift,使用Swift的本机字典和数组
,而不是NSDictionary
和NSArray
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)
}
})
谢谢你的回复,它可以工作,但我仍然有一个问题:应该是什么样子:是这样出来的:http:\/\/dg7csqkdaad2n.cloudfront.net\/Media\/2262089580297_1494328417.jpeg,我没有解析文件的内容,所以我无法以编程方式修复它。有什么想法吗?谢谢again@user3411226不了解您的问题文件中的URL如下所示:http:\/\/dg7csqkdaad2n.cloudfront.net\/Media\/2262089580297_1494328417.jpeg,其中给出错误且不正确accessible@user3411226该文件包含这样的url或它的url,因为它是以编程方式存储JSON的?这是在写入该文件后以编程方式存储的