0

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

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

字典解析

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

 }
4

2 回答 2

1

我猜您的字典不包含“位置”,这与我从该网站返回的内容一致。它发回包含字典数组的字典数组。您需要提取其中一个内部词典才能找到“位置”。

[

    {
        "date":1320883200000,
        "events":[
            {
                "affectedDate":1320883200000,
                "event":{
                    "appId":620,
                    "eventId":20672,
                    "location":"Samsen Kulturhus",
                    "municipality":"Kristiansand",
                    "title":"AKKS kurs høsten 2011"
                }
            },
         ....
于 2011-11-09T02:11:03.263 回答
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;
}
于 2012-02-14T11:12:24.647 回答