遍历从webservice,服务器上面的json文件中数据
---解析json字符串---:
-(void)parseJsonKit:(NSString *)jsonString
{
NSArray *array=[jsonStringobjectFromJSONString];
NSLog(@"yunPanCounts=%d",[arraycount]);
//云端联系人的数量
self.yunpanCounts.text =[NSStringstringWithFormat:@"%u",[arraycount]];
for (int i =0; i< array.count; i++) {
NSDictionary* temp = [arrayobjectAtIndex:i];
NSString *firtname = [tempobjectForKey:@"firstname"];
NSString *lastname = [tempobjectForKey:@"lastname"];
NSString *middleName = [tempobjectForKey:@"middleName"];
NSString *prefix = [tempobjectForKey:@"prefix"];
NSString *suffix = [tempobjectForKey:@"suffix"];
NSString *firstnamePhonetic = [tempobjectForKey:@"firstnamePhonetic"];
NSString *lastnamePhonetic = [tempobjectForKey:@"lastnamePhonetic"];
NSString *organization = [tempobjectForKey:@"organization"];
NSString *jobtitle = [tempobjectForKey:@"jobtitle"];
NSString *department = [tempobjectForKey:@"department"];
NSString *note = [tempobjectForKey:@"note"];
NSString *birthday = [tempobjectForKey:@"birthday"];
NSString *firstKnow = [tempobjectForKey:@"firstKnow"];
NSString *lastKnow = [tempobjectForKey:@"lastKnow"];
//add to contact
ABAddressBookRef iphoneAddressBook =ABAddressBookCreate();
ABRecordRef newPerson =ABPersonCreate();
CFErrorRef error =NULL;
ABRecordSetValue(newPerson,kABPersonFirstNameProperty,firtname, &error);
ABRecordSetValue(newPerson,kABPersonLastNameProperty,lastname, &error);
ABRecordSetValue(newPerson,kABPersonMiddleNameProperty, middleName, &error);
ABRecordSetValue(newPerson,kABPersonPrefixProperty, prefix, &error);
ABRecordSetValue(newPerson,kABPersonSuffixProperty, suffix, &error);
ABRecordSetValue(newPerson,kABPersonFirstNamePhoneticProperty, firstnamePhonetic, &error);
ABRecordSetValue(newPerson,kABPersonLastNamePhoneticProperty, lastnamePhonetic, &error);
ABRecordSetValue(newPerson,kABPersonOrganizationProperty, organization, &error);
ABRecordSetValue(newPerson,kABPersonJobTitleProperty, jobtitle, &error);
ABRecordSetValue(newPerson,kABPersonDepartmentProperty, department, &error);
ABRecordSetValue(newPerson,kABPersonNoteProperty, note, &error);
ABRecordSetValue(newPerson,kABPersonBirthdayProperty, birthday, &error);
ABRecordSetValue(newPerson,kABPersonCreationDateProperty, firstKnow, &error);
ABRecordSetValue(newPerson,kABPersonModificationDateProperty, lastKnow, &error);
///////////////////特别值的注意的是多值字段的遍历///////////////////////////////
//用于存放具有多个值的项
ABMutableMultiValueRef multi =ABMultiValueCreateMutable(kABMultiStringPropertyType);
//得到多值字段[电话号码]的集合
NSMutableDictionary *phoneDict = [tempobjectForKey:@"phone"];
NSLog(@"%@",phoneDict);
for (id keyin phoneDict)
{
if ([keyisEqualToString:@"home"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonHomePageLabel,NULL);
}
if ([keyisEqualToString:@"main"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneMainLabel,NULL);
}
if ([keyisEqualToString:@"work fax"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneWorkFAXLabel,NULL);
}
//other
if ([keyisEqualToString:@"mobile"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneMobileLabel,NULL);
}
if ([keyisEqualToString:@"home fax"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneHomeFAXLabel,NULL);
}
if ([keyisEqualToString:@"other fax"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneOtherFAXLabel,NULL);
}
if ([keyisEqualToString:@"pager"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhonePagerLabel,NULL);
}
if ([keyisEqualToString:@"iPhone"]) {
ABMultiValueAddValueAndLabel(multi, [phoneDict objectForKey:key], kABPersonPhoneIPhoneLabel,NULL);
}
ABRecordSetValue(newPerson,kABPersonPhoneProperty, multi, &error);
}
//清空该变量用于存放下一个多值的项
multi = nil;
////////////////////////////////////////////////////////////////////////////////
ABAddressBookAddRecord(iphoneAddressBook, newPerson, &error);
ABAddressBookSave(iphoneAddressBook, &error);
if (error!=NULL) {
NSLog(@"Danger will Robinson! Danger!!!");
}
CFRelease(newPerson);
CFRelease(iphoneAddressBook);
}
}