2012-05-28 97 views
0

我在下面的方式編寫代碼。如何獲取解析值?

NSDictionary* json = [NSJSONSerialization 
    JSONObjectWithData:responseData 
options:kNilOptions error:&error]; 

NSLog(@"%@",json); 

印刷字典是

(
     { 
     Contents =   (
         { 
       Id = 2; 
       LastUpdated = "/Date(1338048712847+0000)/"; 
       Title = "Webinar: HP & MS solutions for Mid-Market"; 
       Url = "http://infra2apps.blob.core.windows.net/content/VMMM019-HP-MS_MidMarket.wmv"; 
      }, 
         { 
       Id = 1; 
       LastUpdated = "/Date(1338048712773+0000)/"; 
       Title = "Webinar: Private Cloud with HP & MS"; 
       Url = "http://infra2apps.blob.core.windows.net/content/VMPC012-HPMS_PrivateCloud.wmv"; 
      } 
     ); 
     Id = 1; 
     ImageUrl = "http://infra2apps.blob.core.windows.net/eventapp/black-microsoft-logo.jpg"; 
     Name = "Unified Communications & Collaborations"; 
     Sessions =   (
         { 
       Description = "Microsoft Lync delivers Unified Communication to help People connect in new ways, anytime, anywhere. Learn how HP and Microsoft are helping customers transform their business infrastrucutre and gain greater productivity by making every communication an interaction that is more collaborative and engaging."; 
       EndDate = "/Date(1275822000000+0000)/"; 
       FriendlyName = TB3257; 
       Id = 1; 
       Location = "Building N-4105"; 
       Speakers =     (
             { 
         Company = Microsoft; 
         Email = "[email protected]"; 
         Name = "John Doe"; 
         Title = "Group Manager"; 
        } 
       ); 
       StartDate = "/Date(1275818400000+0000)/"; 
       Title = "Connecting People in New Ways with Microsoft Lync"; 
      }, 
         { 
       Description = "Microsoft Lync delivers Unified Communication to help People connect in new ways, anytime, anywhere. Learn how HP and Microsoft are helping customers transform their business infrastrucutre and gain greater productivity by making every communication an interaction that is more collaborative and engaging."; 
       EndDate = "/Date(1275825600000+0000)/"; 
       FriendlyName = TB3258; 
       Id = 2; 
       Location = "Building N-4105"; 
       Speakers =     (
             { 
         Company = HP; 
         Email = "[email protected]"; 
         Name = "Jane Doe"; 
         Title = "Vice President"; 
        }, 
             { 
         Company = Microsoft; 
         Email = "[email protected]"; 
         Name = "John Doe"; 
         Title = "Group Manager"; 
        } 
       ); 
       StartDate = "/Date(1275822000000+0000)/"; 
       Title = "Connecting People in New Ways with Microsoft Lync - Part 2"; 
      } 
     ); 
    }, 

....等

然後含量值存儲到另一字典之後,我存儲到一個數組。 下面的代碼是存儲陣列ID

NSDictionary *boothmenucontents = [json valueForKey: @"Contents"]; 
    NSMutableArray *dictResponseboothmenucontentsArray = [[NSMutableArray alloc] initWithObjects: boothmenucontents,nil]; 
for(int i = 0; i<[dictResponseboothmenucontentsArray count]; i++) 

    { 
     NSMutableArray *IdArrayboothmenucontentes=[[dictResponseboothmenucontentsArray objectAtIndex:i] valueForKey:@"Id"]; 

     NSLog(@"id array is %@",IdArrayboothmenucontentes); 
     for(int k=0;k<[IdArrayboothmenucontentes count];k++) 
     { 
      NSString * strcontentId= [NSString stringWithFormat:@"%@",[IdArrayboothmenucontentes objectAtIndex:k]]; 

      NSLog(@"strcontentId%@",strcontentId); 
      label.text=strcontentId; 
      [boothmenuidarrayvalues addObject:strcontentId]; 


      NSLog(@"%@",boothmenuidarrayvalues); 

     } 


    } 

最後我打印boothmenuidarrayvalues

其打印這樣

"(\n 2,\n 1\n)", 
    "(\n 4,\n 3\n)", 
    "(\n 6,\n 5\n)", 
    "(\n 8,\n 7\n)", 
    "(\n 10,\n 9\n)", 
    "(\n 12,\n 11\n)" 

,但我想打印內容標識只有一次,但它在打印連續兩次。 可能是我遵循一個錯誤的方法,請告訴我如何給自己的根源,該響應。

請幫我.......

+0

可以打印IdArrayboothmenucontentes ..? –

+0

@safecase是的,但它一次需要兩個id值 – kumar

+0

試試這個:NSDictionary * boothmenucontents = [json valueForKey:@「Contents」]; NSMutableArray * dictResponseboothmenucontentsArray = [[NSMutableArray alloc] initWithObjects:boothmenucontents,nil]; 的for(int i = 0; I <[dictResponseboothmenucontentsArray計數];我++) {[boothmenuidarrayvalues ADDOBJECT:[[dictResponseboothmenucontentsArray objectAtIndex:ⅰ] valueForKey:@ 「ID」];} –

回答

0

這行看起來錯誤:

NSMutableArray *IdArrayboothmenucontentes=[[dictResponseboothmenucontentsArray objectAtIndex:i] valueForKey:@"Id"]; 

我不知道你是希望它做什麼,但它看起來像它抓取dictResponseboothmenucontentsArray(它是一個字典)中的第i個對象,然後用鍵「Id」(這是一個數字)獲取該字典中的對象,所以IdArrayboothmenucontentes現在包含一個NSNumber,而不是一個數組。

1

也許它會幫助你。

NSMutableArray *contenstsArray = [contentsDictionary ValueForKey:@"Contents"]; //Suppose you already brought all the json data into contentsDictionary 
NSMutableArray *idArray = [[NSMutableArray alloc] init]; 
for(NSMutableDictionary *idDic in contenstsArray) { 
    NSString *idString = [idDic valueForKey:@"Id"]; 
    [idArray addObject:]; 
}