2011-07-23 81 views
0

我解析它會在字典中的所有數據,但是當我檢查使用的NSLog它昭和零值解析JSON顯示零值

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價值解析

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

} 
+0

你是如何設置從詞典中'location'財產? –

+0

是的,我通過follwoing的方式設置 – Ali

+0

我已經添加了上面的代碼 – Ali

回答

1

我的數據猜測你的字典不包含「位置」,這與我從該網站返回的內容一致。它返回一個包含字典數組的字典數組。您需要提取其中一個內部詞典以查找「位置」。

[ 

    { 
     "date":1320883200000, 
     "events":[ 
      { 
       "affectedDate":1320883200000, 
       "event":{ 
        "appId":620, 
        "eventId":20672, 
        "location":"Samsen Kulturhus", 
        "municipality":"Kristiansand", 
        "title":"AKKS kurs høsten 2011" 
       } 
      }, 
     .... 
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; 
}