Ios + JSON = NSJSONSerialization

Ios 5 provides you with a cool library for json parsing without having to search for
third party libraries.

Here is an example of how to use the NSJSONSerialization class, in order to parse or do some json serialization

//
//  JsonTest.h
//  JsonTest
//
//  Created by gkatzioura on 1/14/13.
//  Copyright (c) 2013 Gkatziouras Emmanouil. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface JsonTest : NSObject
-(NSString*)getjSon;
-(void)parseJson:(NSString*)jsonString;
@end
//
//  JsonTest.m
//  JsonTest
//
//  Created by gkatzioura on 1/14/13.
//  Copyright (c) 2013 Gkatziouras Emmanouil. All rights reserved.
//

#import "JsonTest.h"

@implementation JsonTest
-(NSString*)getjSon {

    NSMutableDictionary* mineDictionary = [[NSMutableDictionary alloc] init];

    [mineDictionary setObject:@"gkatzioura" forKey:@"username"];
    [mineDictionary setObject:@"Gkatziouras" forKey:@"surname"];
    [mineDictionary setObject:@"Emmanouil" forKey:@"name"];

    NSMutableDictionary* yourDictionary = [[NSMutableDictionary alloc] init];

    [yourDictionary setObject:@"dunno" forKey:@"username"];
    [yourDictionary setObject:@"dunno" forKey:@"surname"];
    [yourDictionary setObject:@"dunno" forKey:@"name"];

    NSMutableArray* jsonArray = [[NSMutableArray alloc] init];

    [jsonArray addObject:mineDictionary];
    [jsonArray addObject:yourDictionary];

    NSData* nsdata = [NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONReadingMutableContainers error:nil];

    NSString* jsonString =[[NSString alloc] initWithData:nsdata encoding:NSUTF8StringEncoding];

    return jsonString;
}

-(void)parseJson:(NSString *)jsonString {

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

    for(NSDictionary* account in jsonArray) {

        NSLog([account objectForKey:@"username"]);
        NSLog([account objectForKey:@"surname"]);
        NSLog([account objectForKey:@"name"]);
    }

}
@end

And you just do

#import "JsonTest.h"

.
.
.
.

JsonTest* jsonTest = [[JsonTest alloc] init];

NSString* wut = [jsonTest getjSon];

//NSLog(wut);

[jsonTest parseJson:wut];

NSJSONSerialization reference