data(withJSONObject:options:)
Returns JSON data from a Foundation object.
Declaration
class func data(withJSONObject obj: Any, options opt: JSONSerialization.WritingOptions = []) throws -> DataParameters
- obj:
The object from which to generate JSON data. Must not be
nil. - opt:
Options for creating the JSON data.
See Writingoptions for possible values.
Return Value
JSON data for obj, or nil if an internal error occurs. The resulting data is encoded in UTF-8.
Discussion
If obj can’t produce valid JSON, JSONSerialization throws an exception. This exception occurs prior to parsing and represents a programming error, not an internal error. Before calling this method, you should check whether the input can produce valid JSON by using isValidJSONObject(_:).
Setting the prettyPrinted option generates JSON with white space designed to make the output more readable. If this option isn’t set, JSONSerialization generates the most compact possible JSON.