Swift – Convert [[String:Any?]] to Data

i0S Swift Issue

Question or problem in the Swift programming language:

I have an [[String:Any]] object populated like:

var result : [[String : Any]] = [[String : Any]]()

And I need convert it to Data.

I’m just using:
JSONEncoder().encode(result)
To convert it.

But I get this error:

Exist a simple way to convert a [[String:Any?]] object toData` ?

How to solve the problem:

JSONEncoder can only encode objects whose type conforms to Encodable. If you want to encode Any to JSON, you need to use JSONSerialization to do that.

let jsonData = try? JSONSerialization.data(withJSONObject:result)

Hope this helps!