0

I am parsing the data it gets all the data in dictionary but when i check value using NSLog it showa nil value

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder"]];
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];
//appDelegate.books = [[NSMutableArray alloc] initWithCapacity:0];

appDelegate.books1 = [[NSMutableArray alloc] init];


NSArray *results = [parser objectWithString:json_string error:nil];
for (int i=0; i<[results count]; i++) {
    Book1  *aBook = [[Book1 alloc] initWithDictionary:[results objectAtIndex:i]];
    [appDelegate.books1 addObject:aBook];
    Book1 *mybook=[appDelegate.books1 objectAtIndex:i];
    NSString*test=mybook.location;
    NSLog(test);
}

Dicionary parsing

 - (id)initWithDictionary:(NSDictionary*) dict {

    self.date = [dict valueForKey:@"date"];
    self.location =  [dict valueForKey:@"location"];
    self.municipality = [dict valueForKey:@"municipality"];
    self.title =  [dict valueForKey:@"title"];

    return self;

 }
Sebastien Peek
  • 2,528
  • 2
  • 23
  • 32
Ali
  • 1,951
  • 2
  • 14
  • 14
  • How are you setting the `location` property from the dictionary? – Deepak Danduprolu Jul 23 '11 at 06:34
  • yes i am setting by follwoing way – Ali Jul 23 '11 at 06:45
  • I am assuming that the `NSLog(test);` part is showing `nil`. That would imply `results` and `Book1` instances are not `nil`. Have you tried putting `NSLog(@"%@', dict);` in the `initWithDictionary:` method to see if you are getting all the values? If possible add the `dict` for one of the items. Actually, where are you getting your JSON data from? – Deepak Danduprolu Jul 23 '11 at 06:59
  • http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder – Ali Jul 23 '11 at 07:01
  • i am getting from above added link – Ali Jul 23 '11 at 07:01
  • i am also checked in initWithDict Method it showing nil – Ali Jul 23 '11 at 07:03

2 Answers2

1

I'm guessing that your dictionary doesn't contain a "location", and that's consistent with what I see coming back from that web site. It sends back an array of dictionaries which contain arrays of dictionaries. You need to extract one of those inner dictionaries to find a "location".

[

    {
        "date":1320883200000,
        "events":[
            {
                "affectedDate":1320883200000,
                "event":{
                    "appId":620,
                    "eventId":20672,
                    "location":"Samsen Kulturhus",
                    "municipality":"Kristiansand",
                    "title":"AKKS kurs høsten 2011"
                }
            },
         ....
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

Try:

- (id)initWithDictionary:(NSDictionary*) dict {
   self = [super init];
   if(self) {
      self.date = [dict valueForKey:@"date"];
      self.location =  [dict valueForKey:@"location"];
      self.municipality = [dict valueForKey:@"municipality"];
      self.title =  [dict valueForKey:@"title"];
   }
   return self;
}
dzeikei
  • 2,256
  • 1
  • 21
  • 27