Type Method

dataWithJSONObject:options:error:

Returns JSON data from a Foundation object.

Declaration

+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError * _Nullable *)error;

Parameters

obj

The object from which to generate JSON data. Must not be nil.

opt

Options for creating the JSON data.

See NSJSONWritingOptions for possible values.

error

If an internal error occurs, upon return contains an NSError object with code NSPropertyListWriteInvalidError that describes the problem.

Return Value

JSON data for obj, or nil if an internal error occurs. The resulting data is encoded in UTF-8.

Discussion

If obj will not produce valid JSON, an exception is thrown. This exception is thrown prior to parsing and represents a programming error, not an internal error. You should check whether the input will produce valid JSON before calling this method by using isValidJSONObject:.

Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated.

See Also

Creating JSON Data

+ writeJSONObject:toStream:options:error:

Writes a given JSON object to a stream.

+ isValidJSONObject:

Returns a Boolean value that indicates whether a given object can be converted to JSON data.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software