cannot encode data in ios fetch webservice.
- (nsdata*)encodedictionary:(nsdictionary*)dictionary { nsmutablearray *parts = [[nsmutablearray alloc] init]; (nsstring *key in dictionary) { nsstring *encodedvalue = [[nsstring stringwithformat:@"%@",[dictionary objectforkey:key]] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *encodedkey = [[nsstring stringwithformat:@"%@",key] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *part = [nsstring stringwithformat: @"%@=%@", encodedkey, encodedvalue]; [parts addobject:part]; } nsstring *encodeddictionary = [parts componentsjoinedbystring:@"&"]; return [encodeddictionary datausingencoding:nsutf8stringencoding]; }
it seems encoding nsdictionary , generate nsstring. , again encoding nsstring , returns it.
you may in easy way below.
nserror *error; nsdata *jsondata = [nsjsonserialization datawithjsonobject:dictionaryorarraytooutput options:nsjsonwritingprettyprinted error:&error]; if (! jsondata) { nslog(@"got error: %@", error); } else { nsstring *jsonstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; }
you may return above jsondata in function achieve requirement below.
- (nsdata*)encodedictionary:(nsdictionary*)dictionary { nserror *error; nsdata *jsondata = [nsjsonserialization datawithjsonobject:dictionary options:nsjsonwritingprettyprinted error:&error]; if (! jsondata) { nslog(@"got error: %@", error); } else { return jsondata; } }
Comments
Post a Comment