Ali
Ali

Reputation: 1961

How to store the parsed JSON data in a singl array

I have parsed JSON data the format of my JSON data is http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder

SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *object = [parser objectWithString:json_string error:nil];
    NSArray *results = [parser objectWithString:json_string error:nil];

Upvotes: 0

Views: 395

Answers (2)

Rahul Vyas
Rahul Vyas

Reputation: 28720

How about creating a data modal?

@interface book:NSObject {
NSString *catId;
NSString *bookName
}

create properties for these two instance vars.
@end


@implementation book 

@synthesize catId,bookName;

- (id)init {
 self = [super init];
}

- (id)initWithDictionary:(NSDictionary) dict {
 self.catId = [dict valueForKey:@"categoryId"];
  self.bookName = [dict valueForKey:@"name"];
}

- (void)dealloc {
[catId release];
[bookName release];
[super dealloc];
}
@end


and use it like this

 NSMutableArray *bookArray = [[NSmutableArray alloc] initWithCapacity:0];
 NSArray *results = [parser objectWithString:json_string error:nil];
    for (int i=0; i<[results count]; i++) {
   book *bookObject = [[book alloc] initWithDictionary:[results objectAtIndex:i]];
   [bookArray addObject:bookObject];
   [bookObject release];
}

Upvotes: 1

Sabby
Sabby

Reputation: 2592

I think you can do this by way Adding dictionary to array.

 for (NSDictionary *dict in mydict) {
            [myArray addObject:dict];
        }

You can do more modification and put the logic to set the values in array according to key as well. Hope this may help you.Well I haven't checked it now.But may get you to the solution.

Cheers.....

Upvotes: 0

Related Questions