0

when i try to parse my json with decodable birthday comes nil. What date format should I use any advice or code sample please. my date format include timezone.

My problem is birthdate comes nil. How to parse birthdate with decodable ?

My json :

{
    "id": 1,
    "name": "fatih",
    "birddate": "2018-09-19T11:36:00.4033163+03:00",
    "total": 0.9,
    "isTest": false
}

here is my struct :

struct TestDTO : Decodable {
    var id:Int?
    var name : String?
    var birtdate : Date?
    var total : Double?
    var isTest : Bool?
}

RestClientServiceTest().CallRestService(matching: cmd, completion: { (data) in
            do{

                let decoder = JSONDecoder()
                decoder.dateDecodingStrategy = .formatted(dateFormatter)
                let stories = try decoder.decode(TestDTO.self, from: data!)
                print(data)
            }catch let error{
                print("Json Parse Error : \(error)")
            }
        })
cartoonworld
  • 133
  • 1
  • 1
  • 8

2 Answers2

3

So, having a bit of play in playground...

let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"
let value = "2018-09-19T11:36:00.4033163+03:00"
print(format.date(from: value))

Prints 2018-09-19 08:36:00 +0000

So taking that a leap further...

let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SZ"

let text = """
{
"id": 1,
"name": "fatih",
"birddate": "2018-09-19T11:36:00.4033163+03:00",
"total": 0.9,
"isTest": false
}
"""

struct TestDTO : Decodable {
    var id:Int?
    var name : String?
    var birddate : Date?
    var total : Double?
    var isTest : Bool?
}

do{
    let jsonData = text.data(using: .utf8)
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .formatted(format)
    let stories = try decoder.decode(TestDTO.self, from: jsonData!)
    print(stories)
}catch let error{
    print("Json Parse Error : \(error)")
}

prints...

TestDTO(id: Optional(1), name: Optional("fatih"), birddate: Optional(2018-09-19 08:36:00 +0000), total: Optional(0.9), isTest: Optional(false))

You might find Easy Skeezy Date Formatting for Swift of some use

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Can you also use `birthdate` instead of `birddate`(whatever it is)? – user28434'mstep Sep 19 '18 at 11:20
  • @user28434 You can use what every you want, I just took what was given and made it work - to tired of making assumptions, sorry – MadProgrammer Sep 19 '18 at 11:21
  • Well, as @vadian noticed, question uses 3 different forms, only `birthdate` looks like something. – user28434'mstep Sep 19 '18 at 11:22
  • 1
    @user28434 Your points are valid - but I just focused on getting the date format to work - the OP will need to fix any other inconsistencies - I appreciate this is probably going to annoy some people, about as much as people representing date/time values as strings in code does to me – MadProgrammer Sep 19 '18 at 11:22
0
  1. First of all the name you used in struct is wrong birddate - typo error.
  2. Second don't set it as Date it's an String.

Get the data as String and while using it go with NSDateFormatter.

You can search for NSDateFormatter on SO.

Amit
  • 4,837
  • 5
  • 31
  • 46
  • 2
    It is `Date` represented as `String`, `JSONDecoder.DateDecodingStrategy.formatted()`(and `.iso8601`) exist exactly for this. To prevent you from doing it manually. – user28434'mstep Sep 19 '18 at 11:15
  • @user28434 : That I wasn't aware of. I was using `NSDateFormatter`. Thank you. Should I remove my answer ? – Amit Sep 19 '18 at 11:21