在學校我們正在創建一個程序來跟蹤帆船賽。斯威夫特3字典JSON
我目前正在爲帆船隊開發GPS追蹤器應用程序。 的JSON發送到我們的API必須是這樣的:
{"hash":"asdh832","positions":[{"longitude":13.340532999999999,"latitude":52.4965431,"time":1488182463461},{"longitude":13.3175489,"latitude":52.4927039,"time":1488195535705},{"longitude":13.3175489,"latitude":52.4927039,"time":1488195536657}]}
首先哈希在一個陣列的團隊和位置(如果智能手機不具有互聯網連接的ATM稍後發送它們)
我有一個 「位置」 陣列字典:
var positions = Array<Dictionary<String,String>>()
positions.append( ["longitude":String(location.coordinate.longitude),
"latitude":String(location.coordinate.latitude),
"time":String(Int64(location.timestamp.timeIntervalSince1970 * 1000.0))])
// Times 2 to test
positions.append( ["longitude":String(location.coordinate.longitude),
"latitude":String(location.coordinate.latitude),
"time":String(Int64(location.timestamp.timeIntervalSince1970 * 1000.0))])
let data2 = try JSONSerialization.data(withJSONObject: positions, options: [])
let dataString2 = String(data: data2,encoding: String.Encoding.utf8)!
print(dataString2)
打印的(dataString2)其結果是:
[{"latitude":"52.4965211222075","longitude":"13.3405919673345","time":"1488194768467"},{"latitude":"52.4965211222075","longitude":"13.3405919673345","time":"1488194768467"}]
這是正確的。現在我想將它與哈希合併:
let params: Dictionary<String, String> = ["hash":"asdh832","positions": dataString2]
let data = try JSONSerialization.data(withJSONObject: params , options: [])
let dataString = String(data: data,encoding: String.Encoding.utf8)!
,但現在的結果後「的位置:」看起來有點怪異:
{"hash":"asdh832","positions":"[{\"latitude\":\"52.4966040328522\",\"longitude\":\"13.3402242104124\",\"time\":\"1488195406482\"},{\"latitude\":\"52.4966040328522\",\"longitude\":\"13.3402242104124\",\"time\":\"1488195406482\"}]"}
沒有這些額外的「和\這將是正確的,但我只是不知道如何建立像這樣。
我使用的是斯威夫特3 Xcode的最後手段8.2.1
... omg謝謝!它現在的作品:D –
@SquirrelofPain歡迎隊友:) –