objective c - iOS, create JSON object with ABAddressBookCreate's element -
i'm using abaddressbookcreatewithoptions , abaddressbookcopyarrayofallpeople achieve contacts' informations.
i can person's full name, emails , phone numbers that:
addressbook = abaddressbookcreatewithoptions(null, null); people = abaddressbookcopyarrayofallpeople(addressbook); (cfindex = 0; < cfarraygetcount(people); i++) { abrecordref person = cfarraygetvalueatindex(people, i); ////get full name//// nsstring *fullname = @""; if (abrecordcopyvalue(person, kabpersonfirstnameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@ ", abrecordcopyvalue(person, kabpersonfirstnameproperty)]; } if (abrecordcopyvalue(person, kabpersonmiddlenameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@%@ ", fullname,abrecordcopyvalue(person, kabpersonmiddlenameproperty)]; } if (abrecordcopyvalue(person, kabpersonlastnameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@%@", fullname,abrecordcopyvalue(person, kabpersonlastnameproperty)]; } fullname = [fullname stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; nslog(@"fullname: %@",fullname); ////get phone numbers//// abmultivalueref phonenumbers = abrecordcopyvalue(person, kabpersonphoneproperty); if (abmultivaluegetcount(phonenumbers)>0) { (cfindex j=0; j < abmultivaluegetcount(phonenumbers); j++) { nsstring *phonenumber = (nsstring*)cfbridgingrelease(abmultivaluecopyvalueatindex(phonenumbers, j)); nslog(@"phone number: %@",phonenumber); } } cfrelease(phonenumbers); ////get emails//// abmultivalueref emails = abrecordcopyvalue(person, kabpersonemailproperty); if (abmultivaluegetcount(emails)>0) { (cfindex j=0; j < abmultivaluegetcount(emails); j++) { nsstring *email = (nsstring*)cfbridgingrelease(abmultivaluecopyvalueatindex(emails, j)); nslog(@"email: %@",email); } } cfrelease(emails); } cfrelease(addressbook); cfrelease(people);
everything works perfectly. need create json object these informations that:
[{"name":"christine work","phone_numbers":["+99023424234"]},{"name":"alex bla","phone_numbers":["+135352125262","+13433452347"],"email_addresses":["bla@bla.com","bla2@bla2.com"]}]
scenario: if person has email address, add json object, if not, not incude in json.
if person has more 1 phone number or more 1 email address add of them json.
i'm stuck right here. know how can create json object nsdictionary :
nserror *error; nsdictionary* info = [nsdictionary dictionarywithobjectsandkeys: @"alex", @"name", @"+90225252", @"phones", nil]; nsdata* jsondata = [nsjsonserialization datawithjsonobject:dictionary options:nsjsonwritingprettyprinted error:&error]; nsstring *jsonstring = [[nsstring alloc] initwithdata:info encoding:nsutf8stringencoding];
but how can integrate code scenario in loop.
give go. added necessary code create json on top of code. if there no phone numbers or emails contact, nsnull added key. make sure check when pulling data out of json. didn't build code, let me know if run errors.
nsmutablearray *usersarray = [[nsmutablearray alloc] init]; nsmutabledictionary *singleuserdictionary = [[nsmutabledictionary alloc] init]; nsmutablearray *phonenumbersarray = [[nsmutablearray alloc] init]; nsmutablearray *emailarray = [[nsmutablearray alloc] init]; (cfindex = 0; < cfarraygetcount(people); i++) { abrecordref person = cfarraygetvalueatindex(people, i); ////get full name//// nsstring *fullname = @""; if (abrecordcopyvalue(person, kabpersonfirstnameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@ ", abrecordcopyvalue(person, kabpersonfirstnameproperty)]; } if (abrecordcopyvalue(person, kabpersonmiddlenameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@%@ ", fullname,abrecordcopyvalue(person, kabpersonmiddlenameproperty)]; } if (abrecordcopyvalue(person, kabpersonlastnameproperty)!=null){ fullname = [nsstring stringwithformat:@"%@%@", fullname,abrecordcopyvalue(person, kabpersonlastnameproperty)]; } fullname = [fullname stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; nslog(@"fullname: %@",fullname); [singleuserdictionary setobject:fullname forkey:@"name"]; ////get phone numbers//// abmultivalueref phonenumbers = abrecordcopyvalue(person, kabpersonphoneproperty); if (abmultivaluegetcount(phonenumbers)>0) { (cfindex j=0; j < abmultivaluegetcount(phonenumbers); j++) { nsstring *phonenumber = (nsstring*)cfbridgingrelease(abmultivaluecopyvalueatindex(phonenumbers, j)); nslog(@"phone number: %@",phonenumber); [phonenumbersarray addobject:phonenumber]; } } else [phonenumbersarray addobject:[nsnull null]]; [singleuserdictionary setobject:phonenumbersarray forkey:@"phone_numbers"]; cfrelease(phonenumbers); ////get emails//// abmultivalueref emails = abrecordcopyvalue(person, kabpersonemailproperty); if (abmultivaluegetcount(emails)>0) { (cfindex j=0; j < abmultivaluegetcount(emails); j++) { nsstring *email = (nsstring*)cfbridgingrelease(abmultivaluecopyvalueatindex(emails, j)); nslog(@"email: %@",email); [emailarray addobject:email]; } } else [emailarray addobject:[nsnull null]]; [singleuserdictionary setobject:emailarray forkey:@"email_addresses"]; cfrelease(emails); [usersarray addobject:[nsdictionary dictionarywithdictionary:singleuserdictionary]]; [singleuserdictionary removeallobjects]; [phonenumbersarray removeallobjects]; [emailarray removeallobjects]; } nserror *error = nil; nsdata *data = [nsjsonserialization datawithjsonobject:usersarray options:0 error:&error]; if (error) { //json success } cfrelease(addressbook); cfrelease(people);
Comments
Post a Comment