-5

I have a problem when it comes to taking json from a server an example of the json looks like this:

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

I don't know what kind of object this would make and quite frankly how to desteralize it, if you could help me it would be greatly appreciated.

JAL
  • 41,701
  • 23
  • 172
  • 300
KyleThe1st
  • 19
  • 4
  • 2
    that is a dictionary inside an array in swift terms. https://developer.apple.com/swift/blog/?id=37 – luk2302 May 25 '17 at 18:58
  • A combination of @luk2302s comment and my answer [here](https://stackoverflow.com/questions/43700443/how-to-write-own-model-mapper-in-swift-language/43700629#43700629) you will solve this. – Rashwan L May 25 '17 at 19:01
  • Convert JSON Into Dicotionary ,,,, here is your solution https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary – Dhiru May 25 '17 at 19:03

1 Answers1

0

Most important: Read the JSON. It's pretty easy:

  • {} is a dictionary ([String:Any]).
  • [] is an array ([[String:Any]] sometimes [Any]).
  • All keys are required to be String.
  • All values in double quotes are String.
  • Numeric values with decimal places are Double.
  • Numeric values without decimal places are Int.
  • true / false (not in double quotes) is Bool.
  • null is NSNull.

That's the entire type set of JSON.


Use the (NS)JSONSerialization class to make an object from the data.
There are hundreds (I guess thousands) of examples here on SO.

vadian
  • 274,689
  • 30
  • 353
  • 361