解析JSON显示nil值

时间:2011-07-23 06:21:10

标签: iphone objective-c sbjson

我正在解析数据,它获取字典中的所有数据,但是当我使用NSLog检查值时,它显示零值

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;

 }

1 个答案:

答案 0 :(得分:0)

尝试:

- (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;
}