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 = "johndoe@microsoft.com";
                        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 = "janedoe@hp.com";
                        Name = "Jane Doe";
                        Title = "Vice President";
                    },
                                        {
                        Company = Microsoft;
                        Email = "johndoe@microsoft.com";
                        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);

        }


    }

最後に、ブースメニューID配列値を出力します

このように印刷されます

  "(\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)"

しかし、コンテンツIDを1回だけ印刷したいのですが、2回続けて印刷されます。私は間違った方法に従っている可能性があります。その応答に独自のルートを与える方法を教えてください。

私を助けてください.......

4

2 に答える 2

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:];
}
于 2012-06-19T09:22:42.447 に答える
0

この行は間違っているように見えます:

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

何を期待しているのかわかりませんが、辞書である dictResponseboothmenucontentsArray の i 番目のオブジェクトを取得し、その辞書のオブジェクトをキー「Id」で取得します。数値であるため、IdArrayboothmenucontentes には配列ではなく NSNumber が含まれるようになりました。

于 2012-06-07T00:56:17.143 に答える