2017-08-27 94 views
2

我想将一个swift对象转换为JSON,我已经看到这些问题1 & 2,但我无法将它应用于我的代码。将swift对象(带嵌套对象)转换为纯Swift 3方式中的JSON数据

我有一个类型为[String : DailyTimes]的swift对象,我想以纯Swift 3方式将其转换回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数据(从斯威夫特对象)会是这个样子:

[ 
    "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 
      ] 
     } 

    } 
} 

这是我尝试转换为JSON数据失败的地方。

步骤2:该功能可转换为JSON数据

func convertTimesObjectToJSON(timesObject: [String : DailyTimes]){ 

       for (key, value) in timesObject{ 

        let dictionary = value.convertToDictionary 

        print("dictionary", dictionary) 
        do{ 
         let theJSONData = try JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted) 
        } catch let error{ 
         print("error: \(error)") 
        } 



       } 

      } 
+0

改变这一行'让字典= value.convertToDictionary()' –

回答

3

此方法:

JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted) 

要求dictionary是一个 “属性列表”。和一个这样的:

 [ 
      "weekday" : self.weekday, 
      "available" : self.available, 
      "times"  : self.times 
     ] 

不是属性列表。

为什么?因为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()) 
    ] 
} 
2

试试这个:

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() 
     ] 
    } 
} 

功能转换成JSON:

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)") 
    } 
}