我正在以下方式编写代码。
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);
}
}
最后我打印了boothmenididarrayvalues
它打印得像这样
"(\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,但它连续两次打印。 可能是我遵循了错误的方法,请告诉我如何为该回复提供自己的根。
请帮助我.......
答案 0 :(得分: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:];
}
答案 1 :(得分:0)
这一行看起来不对:
NSMutableArray *IdArrayboothmenucontentes=[[dictResponseboothmenucontentsArray objectAtIndex:i] valueForKey:@"Id"];
我不确定你期望它做什么,但它看起来像是抓住dictResponseboothmenucontentsArray中的第i个对象,这是一个字典,然后使用键“Id”获取该字典中的对象,这是一个数字,所以IdArrayboothmenucontentes现在包含一个NSNumber,而不是一个数组。