iOS Developer Library

Developer

Foundation Framework Reference NSJSONSerialization Class Reference

Options
Deployment Target:

On This Page
Language:

NSJSONSerialization

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

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary.

  • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

  • All dictionary keys are instances of NSString.

  • Numbers are not NaN or infinity.

Other rules may apply. Calling isValidJSONObject: or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

  • Options used when creating Foundation objects from JSON data—see JSONObjectWithData:options:error: and JSONObjectWithStream:options:error:.

    Declaration

    Swift

    struct NSJSONReadingOptions : OptionSetType { init(rawValue rawValue: UInt) static var MutableContainers: NSJSONReadingOptions { get } static var MutableLeaves: NSJSONReadingOptions { get } static var AllowFragments: NSJSONReadingOptions { get } }

    Objective-C

    enum { NSJSONReadingMutableContainers = (1UL << 0), NSJSONReadingMutableLeaves = (1UL << 1), NSJSONReadingAllowFragments = (1UL << 2) }; typedef NSUInteger NSJSONReadingOptions;

    Constants

    • MutableContainers

      NSJSONReadingMutableContainers

      Specifies that arrays and dictionaries are created as mutable objects.

      Available in iOS 5.0 and later.

    • MutableLeaves

      NSJSONReadingMutableLeaves

      Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.

      Available in iOS 5.0 and later.

    • AllowFragments

      NSJSONReadingAllowFragments

      Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary.

      Available in iOS 5.0 and later.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later.

  • Options for writing JSON data.

    Declaration

    Swift

    struct NSJSONWritingOptions : OptionSetType { init(rawValue rawValue: UInt) static var PrettyPrinted: NSJSONWritingOptions { get } }

    Objective-C

    enum { NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions;

    Constants

    • PrettyPrinted

      NSJSONWritingPrettyPrinted

      Specifies that the JSON data should be generated with whitespace designed to make the output more readable. If this option is not set, the most compact possible JSON representation is generated.

      Available in iOS 5.0 and later.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 5.0 and later.