ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBook); if (peoples == nil) { return nil; } CFMutableArrayRef peoplesSorted = CFArrayCreateMutableCopy( kCFAllocatorDefault, CFArrayGetCount(peoples), peoples ); CFArraySortValues( peoplesSorted, CFRangeMake(0, CFArrayGetCount(peoplesSorted)), (CFComparatorFunction) ABPersonComparePeopleByName, (void*) ABPersonGetSortOrdering() ); //person CFRelease(peoples); CFIndex personTotalNumber = CFArrayGetCount(peoplesSorted); NSString *jsonString = [NSString stringWithFormat:@"{"];//这里是整个jsonString的最前端。 NSAutoreleasePool *autoRealeasePoll = [[NSAutoreleasePool alloc] init]; for (int personIndex = 0; personIndex < personTotalNumber; personIndex++) { ABRecordRef singlePersonRecord = CFArrayGetValueAtIndex(peoplesSorted, personIndex); NSString *personInformation = [NSString string]; if (singlePersonRecord != nil) { //name CFStringRef cfName = ABRecordCopyCompositeName(singlePersonRecord); NSString *personName; if (cfName == nil) { personName = @"Unnamed"; } else { personName = [NSString stringWithString:(NSString *)cfName]; CFRelease(cfName); } if (personIndex == 0) { personInformation = [NSString stringWithFormat:@"/"%@/":{" , personName]; } else { personInformation = [NSString stringWithFormat:@",/"%@/":{" , personName]; } NSLog(@"name *****************************************%@" , personName); //phone ABMultiValueRef personPhonesRef = ABRecordCopyValue(singlePersonRecord, kABPersonPhoneProperty); if (personPhonesRef != nil) { CFIndex phoneCountPerPerson = ABMultiValueGetCount(personPhonesRef); personInformation = [personInformation stringByAppendingFormat:@"/"phoneNumber/":{"]; if (phoneCountPerPerson != 0) { for (int phoneIndex = 0; phoneIndex < phoneCountPerPerson; phoneIndex++) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(personPhonesRef,phoneIndex); NSString *phoneNumber = [NSString stringWithString:(NSString *)phoneNumberRef]; if (phoneIndex > 0) { personInformation = [personInformation stringByAppendingFormat:@",/"%@/"" , phoneNumber]; } else { personInformation = [personInformation stringByAppendingFormat:@"/"%@/"" , phoneNumber]; } CFRelease(phoneNumberRef); } //personInformation = [personInformation stringByAppendingFormat:@"}"]; } else { personInformation = [personInformation stringByAppendingFormat:@""]; } personInformation = [personInformation stringByAppendingFormat:@"}"]; personInformation = [personInformation stringByAppendingFormat:@",/"email/":{"]; CFRelease(personPhonesRef); } else { personInformation = [personInformation stringByAppendingFormat:@"/"email/":{"]; } //email ABMultiValueRef personEmailsRef = ABRecordCopyValue(singlePersonRecord, kABPersonEmailProperty); if (personEmailsRef != nil) { CFIndex emailCountPerPerson = ABMultiValueGetCount(personEmailsRef); if (emailCountPerPerson == 0) { personInformation = [personInformation stringByAppendingFormat:@""]; } else { for (int emailIndex = 0; emailIndex < emailCountPerPerson; emailIndex ++) { CFStringRef emailNumberRef = ABMultiValueCopyValueAtIndex(personEmailsRef, emailIndex); NSString *email = [NSString stringWithString:(NSString *) emailNumberRef]; if (emailIndex == 0) { personInformation = [personInformation stringByAppendingFormat:@"/"%@/"" , email]; } else { personInformation = [personInformation stringByAppendingFormat:@",/"%@/"" , email]; } CFRelease(emailNumberRef); } } CFRelease(personEmailsRef); personInformation = [personInformation stringByAppendingFormat:@"}"]; } personInformation = [personInformation stringByAppendingFormat:@"}"]; } jsonString = [jsonString stringByAppendingFormat:@"%@" , personInformation]; } jsonString = [jsonString stringByAppendingFormat:@"}"]; NSLog(@"jsonString %@" , jsonString); CFRelease(addressBook); CFRelease(peoplesSorted); [autoRealeasePoll release];