2
var responseString = String(data: data, encoding: .utf8)
var responseDict: [AnyHashable : Any]? = nil
if let anEncoding = responseString?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
   responseDict = try! JSONSerialization.jsonObject(with: anEncoding, options: .mutableContainers) as? [AnyHashable : Any]
}  

responseString is coming out to be :

[{\"message_id\":1916,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-07T23:17:46.169-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}},{\"message_id\":1920,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-08T03:52:15.404-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}}]

responseDict is coming out to be nil.
The corresponding Objective-C code was working just fine.

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • 2
    If your using Swift 4 it would be much better to use the `Codable` protocol – Scriptable Oct 09 '18 at 09:22
  • 2
    That's because `[AnyHashable : Any]` is a Dictionary but your JSON is a Array at top level. Also, avoid `.mutableContainers` with Swift Dictionaries/Array. And `String.Encoding(rawValue: String.Encoding.utf8.rawValue)` => `.utf8` or `String.Encoding.utf8`. should be enough. – Larme Oct 09 '18 at 09:22
  • 1
    @Larme you should post that as an answer – Scriptable Oct 09 '18 at 09:23
  • @Scriptable There are so much questions about JSON parsing in Swift/Objective-C, there should at least have one duplicate because author didn't see the top level type of the response. – Larme Oct 09 '18 at 09:24
  • why is your variable called `anEncoding` if it is **not** an encoding? – luk2302 Oct 09 '18 at 09:25
  • yeah true.. I'll have a look – Scriptable Oct 09 '18 at 09:25
  • `if let anEncoding = responseString?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))` is horrible. `Data` created from an UTF8 string can never be `nil`. Replace it with `let data = Data(responseString!.utf8)` (without `if`) – vadian Oct 09 '18 at 09:30
  • Also to note: using `try!` will not catch the error, it will most likely crash. If you use a do catch statement you can print the error and it will point you to the issue... and prevent a crash – Scriptable Oct 09 '18 at 09:30
  • https://stackoverflow.com/questions/47281375/convert-json-string-to-json-object-in-swift-4/47281832#47281832 – Amit Oct 09 '18 at 11:11

1 Answers1

2

This works fine…

let json = """
[{\"message_id\":1916,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-07T23:17:46.169-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}},{\"message_id\":1920,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-08T03:52:15.404-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}}]
""".data(using: .utf8)!

do {
    let object = try JSONSerialization.jsonObject(with: json, options: [])
    print(object)
} catch {
    print(error)
}

… but don't use JSONSerialization… declare custom objects that represent your data and use Decodable instead

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 1
    Note that the solution works because of the absence of the `as? Dictionary` contrary the code in the question. `as? [[String: Any]]` should have work too. – Larme Oct 09 '18 at 14:27