Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Parsing JSON on iOS in Objective-C


Objective-C's class libraries define the NSJSONSerialization class, which can serialize to and from JSON. It converts JSON to NSDictionary objects of values, with the keys, the names of the slots in the JSON, and the values of their JSON. It's available in iOS 5.0 and later.

How to do it…

Here's a simple example:

NSError* error;
NSDictionary* data = [ NSJSONSerialization
  JSONObjectWithData: json
  options: kNilOptions
  error: &error ];

NSString* call = [ data ObjectForKey: @"call" ];

How it works…

The NSJSONSerialization class has a method, JSONObjectWithData:options:error, that takes an NSString, parsing options, and a place to record errors, and performs JSON parsing. It can accept JSON whose top level is an array or dictionary, returning an NSArray or NSDictionary result respectively. All values must be instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull respectively. If the top-level object is an array, the method returns NSArray; otherwise...