Ios 以纯swift 3方式将swift对象(带有嵌套对象)转换为JSON数据
我正在尝试将swift对象转换为JSON,我已经看到了这些问题,但我无法将其应用到我的代码中 我有一个类型为Ios 以纯swift 3方式将swift对象(带有嵌套对象)转换为JSON数据,ios,swift3,nsjsonserialization,Ios,Swift3,Nsjsonserialization,我正在尝试将swift对象转换为JSON,我已经看到了这些问题,但我无法将其应用到我的代码中 我有一个类型为[String:DailyTimes]的swift对象,我想用纯swift3方式将其转换回JSON数据,而不需要任何库 下面是我的自定义类: class AvailabilityTimes:{ struct Times{ var startTime : String? var endTime : String? } str
[String:DailyTimes]
的swift对象,我想用纯swift3方式将其转换回JSON数据,而不需要任何库
下面是我的自定义类:
class AvailabilityTimes:{
struct Times{
var startTime : String?
var endTime : String?
}
struct DailyTimes{
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times: [Times]){
self.times = times
}
}
}
转换的JSON数据(来自Swift对象)如下所示:
[
"Monday": [ "weekday" : "Monday",
"available" : true,
"times": [
["startTime": "9:00 AM", "endTime": "1:30 PM" ],
["startTime": "2:30 PM", "endTime": "6:00 PM" ],
["startTime": "7:30 PM", "endTime": "9:00 PM" ]
]
],
"Tuesday": [ "weekday" : "Tuesday",
"available" : true,
"times": [
["startTime": "9:00 AM", "endTime": "6:00 PM" ]
]
]
]
我将[String:DailyTimes]
转换为JSON数据的尝试失败
步骤1:在两个结构中都添加了convertToDictionary函数
class AvailabilityTimes:{
struct Times{
var startTime : String?
var endTime : String?
func convertToDictionary() -> Dictionary<String, Any> {
return [
"startTime" : self.startTime,
"endTime" : self.endTime
]
}
}
struct DailyTimes{
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times: [Times]){
self.times = times
}
func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times
]
}
}
}
此方法:
JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
要求字典
为“属性列表”。一个是:
[
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times
]
不是属性列表
func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times.map{$0.convertToDictionary())
]
}
为什么??因为self.times
的类型为[times]
,而不是属性列表类型。您需要在此处调用self.times.map{$0.convertToDictionary())
,以便将times
转换为属性列表
func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times.map{$0.convertToDictionary())
]
}
func convertToDictionary()->字典{
返回[
“工作日”:self.weekday,
“可用”:自可用,
“times”:self.times.map{$0.convertToDictionary())
]
}
试试这个:
struct DailyTimes{
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times: [Times]){
self.times = times
}
func convertTimesToDictionary() -> [Any] {
var timeDict:[Any] = []
self.times.forEach({ timeDict.append($0.convertToDictionary())})
return timeDict
}
func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.convertTimesToDictionary()
]
}
}
更改此行
let dictionary=value.convertToDictionary()
func convertToJSONObject(timesObject: [String : DailyTimes]) -> [String:AnyObject] {
var dailyTimesObject:[String:AnyObject] = [:]
for (key, value) in timesObject {
dailyTimesObject.updateValue(value.convertToDictionary() as AnyObject, forKey: key)
}
return dailyTimesObject
}
func convertTimesObjectToJSON(timesObject: [String : DailyTimes]){
do{
let theJSONData = try JSONSerialization.data(withJSONObject: convertToJSONObject(timesObject: timesObject), options: .prettyPrinted)
print(String(data: theJSONData, encoding: String.Encoding.utf8)!)
} catch let error{
print("error: \(error)")
}
}