Class: NSJSONSerialization

Inherits:
NSObject show all

Overview

You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

Class Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (NSData) dataWithJSONObject(obj, options:opt, error:error)

Returns JSON data from a Foundation object. If obj will not produce valid JSON, an exception is thrown.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.

Parameters:

  • obj (Object)

    The object from which to generate JSON data.

  • opt (NSJSONWritingOptions)

    Options for creating the JSON data.See “NSJSONWritingOptions” for possible values. Pass 0 to specify no options.

  • error (Pointer)

    If an error occurs, upon return contains an NSError object that describes the problem.

Returns:

  • (NSData)

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

+ (Boolean) isValidJSONObject(obj)

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

Parameters:

  • obj (Object)

    The object to test.

Returns:

  • (Boolean)

    YES if obj can be converted to JSON data, otherwise NO.

+ (Object) JSONObjectWithData(data, options:opt, error:error)

Returns a Foundation object from given JSON data. The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

Parameters:

  • data (NSData)

    A data object containing JSON data.

  • opt (NSJSONReadingOptions)

    Options for reading the JSON data and creating the Foundation objects.For possible values, see “NSJSONReadingOptions.”

  • error (Pointer)

    If an error occurs, upon return contains an NSError object that describes the problem.

Returns:

  • (Object)

    A Foundation object from the JSON data in data, or nil if an error occurs.

+ (Object) JSONObjectWithStream(stream, options:opt, error:error)

Returns a Foundation object from JSON data in a given stream. The data in the stream must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.AvailabilityAvailable in iOS 5.0 and later.See Also + JSONObjectWithData:options:error: + writeJSONObject:toStream:options:error:

Parameters:

  • stream (NSInputStream)

    A stream from which to read JSON data.The stream should be opened and configured.

  • opt (NSJSONReadingOptions)

    Options for reading the JSON data and creating the Foundation objects.For possible values, see “NSJSONReadingOptions.”

  • error (Pointer)

    If an error occurs, upon return contains an NSError object that describes the problem.

Returns:

  • (Object)

    A Foundation object from the JSON data in stream.

+ (Integer) writeJSONObject(obj, toStream:stream, options:opt, error:error)

Writes a given JSON object to a stream.

Parameters:

  • obj (Object)

    The object to write to stream.

  • stream (NSOutputStream)

    The stream to which to write.The stream should be opened and configured.

  • opt (NSJSONWritingOptions)

    Options for writing the JSON data.See “NSJSONWritingOptions” for possible values. Pass 0 to specify no options.

  • error (Pointer)

    If an error occurs, upon return contains an NSError object that describes the problem.

Returns:

  • (Integer)

    The number of bytes written to the stream, or 0 if an error occurs.