【发布时间】:2016-11-21 03:06:18
【问题描述】:
大家好,我有 json 对象。我想在数组中讲述许多 json 对象。我的意思是我想将患者信息保存在 json 对象中。这是工作。但我无法在 json 对象中保存多个患者信息。我应该在数组中插入我的 json 对象吗?
let jsonData = try! JSONSerialization.data(withJSONObject: para, options: JSONSerialization.WritingOptions.init(rawValue: 0))
jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as! String
print(jsonString)
do {
let SavedPatient = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
print("SavedPatient \(SavedPatient)")
if let arrJSON = SavedPatient["contacts"] {
for index in 0...arrJSON.count-1 {
let aObject = arrJSON[index] as! [String : AnyObject]
print("object in array \(aObject)")
}
}
}
catch {
print(error)
}
let utf8str = jsonString.data(using: String.Encoding.utf8)
if let base64Encoded = utf8str?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
{
let encodedData = NSData(base64Encoded: base64Encoded, options:NSData.Base64DecodingOptions(rawValue:0))
//let data = encodedData
if encodedData != nil {
//write
encodedData?.write(toFile: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/offline", atomically: true)
}
}
【问题讨论】:
-
您能否显示您的
JSON是否也在服务器请求中获取此 JSON? -
Not here in comment add response in your question,这也是完整的回复,因为
contacts没有您在问题中添加的内容。 -
如何将数组保存到手机目录?