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

Go To StackoverFlow.com

1

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

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);
    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];
        theObject=nil;

       }

DataController Class

@interface DataController : NSObject {

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


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

@implementation DataController
DataController *theInstance;


+(id)staticVersion
{
    if(!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; 
 }

 @end
2012-04-04 05:18
by Dilshad Almani
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary - Anand 2012-04-04 05:21
http://iphonedevelopertips.com/cocoa/json-framework-for-iphone-part-2.htm - 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-------http://stackoverflow.com/questions/7077007/how-to-parse-a-json-string-in-iphone-objective- - 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: http://stackoverflow.com/questions/8858814/nsjsonserialization-from-nsstrin - o15a3d4l11s2 2012-04-04 08:16


1

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

2012-04-04 08:24
by Haris Hussain


1

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
Ads