Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Essentials

You're reading from   Swift Essentials Get up and running lightning fast with this practical guide to building applications with Swift

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781784396701
Length 228 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Bandlem Limited Bandlem Limited
Author Profile Icon Bandlem Limited
Bandlem Limited
Alex Blewitt Alex Blewitt
Author Profile Icon Alex Blewitt
Alex Blewitt
Arrow right icon
View More author details
Toc

Parsing JSON


The most popular mechanism to send structured data over a network is to encode it in JSON, which stands for JavaScript Object Notation. This provides a hierarchical tree data structure that can store simple numeric, logical, and string-based types along with the array and dictionary representations.

Both Mac OS X and iOS come with a built-in parser for JSON documents, in the NSJSONSerialization class. This provides a means to parse a data object and return an NSDictionary that contains the key/value pairs of a JSON object or an NSArray to represent JSON arrays. Other literals are parsed and represented as the NSNumber or NSString values.

The JSON parser uses JSONObjectWithData to create an object from an NSData object containing a string. This is typically the format returned by network APIs and can be created from an existing string using dataUsingEncoding with one of the built-in encoding types, such as NSUTF8StringEncoding.

A simple JSON array of numbers can be parsed as follows...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image