JSON is a lightweight
data-interchange format, which is more or less human-readable but still
easily machine-parsable. While XML is document-oriented, JSON is data-oriented. If
you need to transmit a highly structured piece of data, you should
probably render it in XML. However, if your data exchange needs are
somewhat less demanding, JSON might be a good option.
The obvious advantage JSON has over XML is that since it is
data-oriented and (almost) parsable as a hash map, there is no requirement
for heavyweight parsing libraries. Additionally, JSON documents are much
smaller than the equivalent XML documents. In bandwidth-limited
situations, such as you might find on the iPhone, this can be important.
JSON documents normally consume around half of the bandwidth as an
equivalent XML document for transferring the same data.
While there is no native support for JSON in the Cocoa Touch
framework, Stig Brautaset’s
json-framework
library implements both a JSON parser and a generator and
can be integrated into your project fairly simply.
If you are dealing exclusively with Rails-based services,
the ObjectiveResource framework (see http://iphoneonrails.com/ for more details) is a port of
the ActiveResource framework of Ruby on Rails. It provides a way to
serialize Rails objects to and from Rails’ standard RESTful web services
via either XML or JSON. ObjectiveResource adds methods to NSObject using the category extension
mechanism, so any Objective-C class can be treated as a remote
resource. |
Download the disk image with the latest version of the
json-framework library from http://code.google.com/p/json-framework/. Open the disk
image and drag and drop the JSON folder into the Classes group in the
Groups & Files pane of your project. Remember to tick the “Copy items
into destination group’s folder” checkbox before adding the files. This
will add the JSON source files to your project; you will still need to
import the JSON.h file into your class to use
it.
Since dynamic linking to third-party embedded frameworks is not
allowed on the iPhone platform, copying the JSON source files into your
project is probably the simplest way to make the parser available to
your application. However, there is a slightly more elegant approach if
you don’t want to add the entire JSON source tree to every project where
you use it. Open the Finder and create an SDKs subfolder inside your home
directory’s Library folder, and copy the JSON folder located inside the
SDKs folder in the disk image into the newly created
~/Library/SDKs directory. Back in Xcode, open your project and double-click on the project
icon at the top of the Groups & Files pane to open the Project Info
window. In the Architectures section in the Build tab, double-click on
the Additional SDKs field and add
$HOME/Library/SDKs/JSON/${PLATFORM_NAME}.sdk to the
list of additional SDKs in the pop-up window. Now go to the Linking
subsection of the Build tab, double-click on the Other Linker Flags
field, and add -ObjC -all_load -ljson
to the flags using the pop-up window. Now you just have to add the following inside your source
file: #import <JSON/JSON.h>
Note the use of angle brackets rather than double quotes around
the imported header file, denoting that it is located in the standard
include path rather than in your project. |
1. The Twitter Search Service
To let you get familiar with the
json-framework library, let’s implement a
bare-bones application to retrieve the trending topics on Twitter by
making use of their RESTful Search API.
Note:
If you’re interested in the Twitter API, you should definitely
look at Twitter’s
documentation for more details regarding the available
methods. However, if you’re serious about using the Twitter API, you
should probably look into using the
MGTwitterEngine library written by Matt Gemmell.
You can download it from http://mattgemmell.com/source.
Making a request to the Twitter Search API of the form http://search.twitter.com/trends.json will return a JSON
document containing the top 10 topics that are currently trending on
Twitter. The response includes the time of the request, the name of each
trend, and the URL to the Twitter Search results page for that
topic:
{
"trends":[
{
"name":"#musicmonday",
"url":"http:\/\/search.twitter.com\/search?q=%23musicmonday"
},
{
"name":"Spotify",
"url":"http:\/\/search.twitter.com\/search?q=Spotify+OR+%23Spotify"
},
{
"name":"Happy Labor Day",
"url":"http:\/\/search.twitter.com\/search?q=%22Happy+Labor+Day%22"
},
{
"name":"District 9",
"url":"http:\/\/search.twitter.com\/search?q=%22District+9%22"
},
{
"name":"Goodnight",
"url":"http:\/\/search.twitter.com\/search?q=Goodnight"
},
{
"name":"Chris Evans",
"url":"http:\/\/search.twitter.com\/search?q=%22Chris+Evans%22"
},
{
"name":"iPhone",
"url":"http:\/\/search.twitter.com\/search?q=iPhone+OR+%23Iphone"
},
{
"name":"Jay-Z",
"url":"http:\/\/search.twitter.com\/search?q=Jay-Z"
},
{
"name":"Dual-Screen E-Reader",
"url":"http:\/\/search.twitter.com\/search?q=%22E-Reader%22"
},
{
"name":"Cadbury",
"url":"http:\/\/search.twitter.com\/search?q=Cadbury"
}
],
"as_of":"Mon, 07 Sep 2009 09:18:34 +0000"
}