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
6
  • xprogress.com/… Commented Apr 4, 2012 at 5:21
  • iphonedevelopertips.com/cocoa/… Commented Apr 4, 2012 at 5: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 given Commented Apr 4, 2012 at 5:24
  • Just have a look at this-------stackoverflow.com/questions/7077007/… Commented Apr 4, 2012 at 5:28
  • i have seen they also given same link of url to get data and parse it as i have done Commented Apr 4, 2012 at 5:40

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.