how to parse json data from a string instead of a URL

Go To


I want to parse a json data which is in NSString how can i do this

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSArray *tempArray =[[DataController staticVersion] startParsing:data];
   for (int i = 0; i<[tempArray count]; i++) {
        id *item = [tempArray objectAtIndex:i];

        NSDictionary *dict = (NSDictionary *) item;
        SearchCode *theObject =[[SearchCode alloc] init];
        [theObject setCodeValue:[dict objectForKey:@"CodeValue"]];
        [theObject setCodeDescription:[dict objectForKey:@"CodeAddedDate"]];    
        [theObject setCodeAddedDate:[dict objectForKey:@"CodeAddedDate"]];
        [theObject setCodeID:[dict objectForKey:@"CodeID"]];
        [theObject setUpdateDateTime:[dict objectForKey:@"UpdateDateTime"]];

        [cptArray addObject:theObject];
        [theObject release];


DataController Class

@interface DataController : NSObject {

+ (id)staticVersion;
- (NSMutableArray *) startParsing:(NSString *)theURLString;

#import "DataController.h"
#import "JSON.h"

@implementation DataController
DataController *theInstance;

    theInstance = [[DataController alloc] init];
return theInstance;

- (NSMutableArray *) startParsing:(NSString *)theURLString {

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",theURLString]];
NSString *fileContent= [NSString stringWithContentsOfURL:url];
SBJSON *parser = [[SBJSON alloc] init];  
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];  
NSArray *items = (NSArray *) data ;  
return items; 

2012-04-04 05:18
by Dilshad Almani - Anand 2012-04-04 05:21 - Anand 2012-04-04 05:23
@Anand i have also followed these ways you can see my code i want instead of url the data should parse from string var as give - Dilshad Almani 2012-04-04 05:24
Just have a look at this------- - Anand 2012-04-04 05:28
i have seen they also given same link of url to get data and parse it as i have don - Dilshad Almani 2012-04-04 05:40
I think this will help you: - o15a3d4l11s2 2012-04-04 08:16


This Post contains classes to parse JSON, XML etc. I have been using these.

2012-04-04 08:24
by Haris Hussain


In the new sdk you do not have to use external classes to parse your JSon you can use NSJSONSerialization witch is Available in iOS 5.0 and later.

To parse a json String using this class you will need to convert your NSString to NSData, you can do that with:

NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];

After that you can use the method to convert the data to json:

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

Your returned type will depend, because it will be like your json, if your json is an array, it will be an array, if is a dictionary, it will be a dictionary, and so on. From apple documentation:

An object that may be converted to JSON must have the following properties:

The top level object is an NSArray or NSDictionary. All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. All dictionary keys are instances of NSString. Numbers are not NaN or infinity.

Hope it help you.

2012-04-04 10:52
by ggrana
the data is already in json format in string which i hav - Dilshad Almani 2012-04-05 04:39
Yes, I understand that, you will need to convert it to NSData, like I put on the first line of code, and after that, you will convert this data to NSArray or NSDictionary, like I put in the second line of code - ggrana 2012-04-05 10:54
Thanks it gives me great help, my data from server is coming in string rather than dictionary or array, so it was difficult to access the key values, but with this code i converted the string into data then json and then dictionary and so on - iPhone Programmatically 2013-03-05 06:31