2016-02-25 58 views
0

我有一段時間試圖將字典對象轉換爲我可以導出到.json文件的字符串。我曾嘗試:JSON字典對象在Swift中的字符串

let data = try NSJSONSerialization.dataWithJSONObject(JSONDictionary, options: .PrettyPrinted) 

這立即失敗:

***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: '無效的類型JSON寫 (__NSDate)'

這裏是我的字典對象:

["latitude": 33.97300288084333, "longitude": -118.4644781426698, "parseID": qJd7QRojdU, "waves": (
     { 
     avgSpeed = 0; 
     course = 0; 
     distance = "43.9934345945615"; 
     favorite = 0; 
     maxSpeed = "5.420000076293945"; 
     time = "7.99999898672104"; 
     timestamp = "2016-02-05 16:05:21 +0000"; 
     wavelocation =   (
         { 
       altitude = 0; 
       course = "78.046875"; 
       latitude = "33.9730028808433"; 
       longitude = "-118.46447814267"; 
       speed = "0.8199999928474426"; 
       timestamp = "2016-02-05 16:05:21 +0000"; 
      }, 
         { 
       altitude = 0; 
       course = "71.3671875"; 
       latitude = "33.9730207342971"; 
       longitude = "-118.464455930626"; 
       speed = "1.080000042915344"; 
       timestamp = "2016-02-05 16:05:22 +0000"; 
      }, 
         { 
       altitude = 0; 
       course = "69.2578125"; 
       latitude = "33.9730514958817"; 
       longitude = "-118.464368926472"; 
       speed = "1.080000042915344"; 
       timestamp = "2016-02-05 16:05:23 +0000"; 
      } 

     ); 
    }, 
     { 
     avgSpeed = 0; 
     course = 0; 
     distance = "112.301783225658"; 
     favorite = 0; 
     maxSpeed = "4.670000076293945"; 
     time = "34.5915005803108"; 
     timestamp = "2016-02-05 16:36:56 +0000"; 
     wavelocation =   (
         { 
       altitude = 0; 
       course = "-1"; 
       latitude = "33.9713087717363"; 
       longitude = "-118.463766921188"; 
       speed = "-1"; 
       timestamp = "2016-02-05 16:36:56 +0000"; 
      }, 
         { 
       altitude = 0; 
       course = "58.8995221253856"; 
       latitude = "33.9713248007833"; 
       longitude = "-118.463470063933"; 
       speed = "3.448220014572144"; 
       timestamp = "2016-02-05 16:37:09 +0000"; 
      }, 
         { 
       altitude = 0; 
       course = "61.875"; 
       latitude = "33.9713574294317"; 
       longitude = "-118.463396206608"; 
       speed = "3.710000038146973"; 
       timestamp = "2016-02-05 16:37:10 +0000"; 
      } 

     ); 
    } 
), "favorite": 0, "idleTime": 0, "paddleDistance": 2392.602, "distance": 0, "locationName": Venice Pier, "duration": 3730, "paddleTime": 412] 

我在這個上撓頭。我已經挖了一段時間(2天)尋找答案,但沒有什麼是彈出。任何幫助表示讚賞! - Rob

+2

值'timeStamp'關鍵似乎是'NSDate',他們應該是'NSString'。這可能是根據您的錯誤消息的問題。你需要轉換它們(使用'NSDateFormatter')。 – Larme

+0

你可以提供你的代碼,你正試圖序列化成JSON的對象嗎? –

回答

1

正如其他人所建議的,你可能需要的任何NSDate對象轉換爲String對象在你的字典中。

假設您需要具體的時間戳格式在您的問題中詳細說明,您可以使用與Unicode Date Formatting的NSDateFormatter。

一些粗糙示例代碼下面給出瞭如何實現這一演示:

// get the current time as an example NSDate 
let now = NSDate() 

// format the date into the timestamp string using NSDateFormatter 
let formatter = NSDateFormatter() 
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx" 
let dateAsString = formatter.stringFromDate(now) 

let dictToConvert = ["name": "Igor", "gender": "male", "time":dateAsString] 

let savePath = NSHomeDirectory() + "/file.json" 

do { 
    let data = try NSJSONSerialization.dataWithJSONObject(dictToConvert, options: .PrettyPrinted) 
    if data.writeToFile(savePath, atomically: true) { 
     print("saved file in location: \(savePath))") 
    } else { 
     print("could not write file to location: \(savePath)") 
    } 
} catch { 
    print("error: \(error)") 
} 
2

處理易於與Web服務交換日期的另一種方法是使用Unix時間戳 - 這是一個數字而不是字符串。

要獲得Unix的時間戳:

let timestamp = date.timeIntervalSince1970 

而且轉換這回日期:

let date = NSDate(timeIntervalSince1970: timestamp) 
+0

這是一個好主意。謝謝! – throb

相關問題