2017-05-25 59 views
-5

我有一個問題,當談到採取JSON從服務器的JSON的一個例子是這樣的:swift怎麼辦?

[{ 
"subject": "First Post", 
"body": "This is a test post coming from mySQL server", 
"user": "user", 
"date": "2017-04-30", 
"comments": "[]", 
"id": 1 }] 

我不知道這會做出什麼樣的對象,並坦率地說,如何desteralize它,如果你能幫助我,將不勝感激。

+2

這是一個數組中的字典,以迅捷的方式。 https://developer.apple.com/swift/blog/?id=37 – luk2302

+0

@ luk2302s評論和我的答案[這裏]的組合(https://stackoverflow.com/questions/43700443/how-to-write- own-model-mapper-in-swift-language/43700629#43700629)你會解決這個問題。 –

+0

將JSON轉換爲Dicotionary ,,,,這裏是你的解決方案https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary – Dhiru

回答

0

最重要:閱讀JSON。這很容易:

  • {}是一本字典([String:Any])。 ([[String:Any]]有時[Any])。
  • 所有的鑰匙必須是String
  • 所有雙引號的值是String
  • 帶小數點的數字值爲Double
  • 無小數位的數字值爲Int。 (不用雙引號)是Bool
  • nullNSNull

這是JSON的整個類型集合。


使用(NS)JSONSerialization類從數據中創建對象。
在SO上有數百個(我猜數千)examples