본문 바로가기

개발도구/iOS - 아이폰 개발

[아이폰] how to use JSON in your app

how to use JSON in your app. (click the images to enlarge)

cocoa-json_step11.) Download the latest version (currently 2.2.1) fromhttp://code.google.com/p/json-framework/downloads/list

cocoa-json_step2

2.) Open the dmg, and drag the JSON folder into your project in Xcode. Check “Copy items into destination group’s folder (if needed)” when prompted.

3.) Once the source is embedded into your project, you need to import the framework to use it in your code

#import "JSON.h"

4.) Create a SBJSON object to parse JSON into a native Cocoa object. SBJSON will return either a NSDictionary or NSArray depending on the structure of the data. You should know ahead of time the structure of the JSON object your parsing and whether it’s a single JSON object or an array of JSON objects and use the appropriate Cocoa class.

// Create SBJSON object to parse JSON
SBJSON *parser = [[SBJSON alloc] init];
    
// parse the JSON string into an object - assuming json_string is a NSString of JSON data
NSDictionary *object = [parser objectWithString:json_string error:nil];
 

Here’s an example showing how to download the public timeline from Twitter as JSON and parse it.

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];

// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses)
{
  // You can retrieve individual values using objectForKey on the status NSDictionary
  // This will print the tweet and username to the console
  NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]);
}