2011-07-25 78 views
0
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]; 
    NSArray *results = [parser objectWithString:json_string error:nil]; 

    NSDictionary *dictOne = [results objectAtIndex:0]; 

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

    NSArray *activitiesArray = [dictOne objectForKey:@"events"]; 



    NSDictionary *dictTwo = [activitiesArray objectAtIndex:0]; 

    NSArray *eventArray=[dictTwo objectForKey:@"event"]; 
    NSDictionary *dictThree=[eventArray objectAtIndex:0]; 



// NSLog(@"%@ - %@", [dictOne objectForKey:@"date"]); 

// NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]); 


    NSString*date=[dictOne objectForKey:@"date"]; 

    NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]); 
    NSLog(@"%@ - %@", [dictThree objectForKey:@"title"]); 
    NSLog(@"%@ - %@", [dictThree objectForKey:@"location"]); 
    NSLog(@"%@ - %@", [dictThree objectForKey:@"municipality"]); 

當我的NSLog(職稱)的位置,直轄市它顯示零值解析JSON數據沒有顯示正確的值

JSON:

[ 
    { 
    "date": 1311552000000, 
    "events": [ 
     { 
     "affectedDate": 1311552000000, 
     "event": { 
      "appId": 42, 
      "eventId": 18095, 
      "location": "Sjølingstad Uldvarefabrik", 
      "municipality": "Lindesnes", 
      "title": "Utstillingsåpning - romfisk" 
     } 
     }, 
     { 
     "affectedDate": 1311552000000, 
     "event": { 
      "appId": 42, 
      "eventId": 18095, 
      "location": "Sjølingstad Uldvarefabrik", 
      "municipality": "Lindesnes", 
      "title": "Utstillingsåpning - romfisk" 
     } 
     }, 
     { 
     "affectedDate": 1311580800000, 
     "event": { 
      "appId": 620, 
      "eventId": 19490, 
      "location": "Høvåg Gjestehus, Vestre Vallesverd, Høvåg", 
      "municipality": "Lillesand", 
      "title": "Kunstutstilling på Høvåg Gjestehus" 
     } 
     }, 
... 
] 

繼普利文答案我已經試過這個代碼,但它仍然是不工作:

NSDictionary *dictOne = [results objectAtIndex:0]; 
NSArray *activitiesArray = [dictOne objectForKey:@"events"]; 
NSDictionary *dictTwo = [activitiesArray objectAtIndex:1]; 
NSArray *eventArray=[dictTwo objectForKey:@"event"]; 
NSDictionary *dictThree=[eventArray objectAtIndex:2]; 
+0

PLS,提供json,所以我們知道它的結構。 – Cyprian

+0

http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder – Ali

+0

這是我正在使用的json數據 – Ali

回答

0
  1. 你不需要初始化解析器,B/C SBJSON使用的NSString類別添加其它解析方法對標準的NSString稱爲JSONValue返回一個NSStringNSDictionary表示。

獲得NSDictionary後,將其轉換爲NSArray像這樣NSArray * values = [dictionary allValues];然後使用方法[values objectForKey:@"date"]獲取日期等。

+0

我使用這樣的,你已經看到代碼的showign日期affecteddate但不顯示其他值 – Ali

+0

你能說清楚的代碼我沒有得到 – Ali

0

其嵌套JSON。值的解析和檢索是不同的。一旦你的字典中值的嵌套對象,你需要深入挖掘它獲得下值。

  1. 從主陣列的getObject事件。
  2. 從上面的解釋
  3. 事件從在步驟2中返回的字典,您可以訪問的appid,EVENTID等獲取對象

所以基本上你的代碼應該是

NSDictionary *dictTwo = [activitiesArray objectAtIndex:1]; // Or get object for key events