ios - The operation couldn’t be completed. (Cocoa error 3840.)" (No value for key in object around character 16.) -


below sample code

 project = @"project";       projectid=@"projectid";       issue =@"issue";       issueid=@"issueid";       activity =@"activity";       activityid=@"activityid";       comment =@"comment";       entryid = @"entryid";       nsmutabledictionary *entryuser = [[nsmutabledictionary alloc] init];       [entryuser setobject:@"5" forkey:common_id];       [entryuser setobject:@"divya bharathi" forkey:common_name];         nsmutabledictionary *sdetails = [[nsmutabledictionary alloc] init];        nsmutablearray *time_entry = [[nsmutablearray alloc] init];     //  nsmutablearray        //self.dataarray = [[nsmutablearray alloc] init];       nsstring *strhours,*wkday;       for(int i=0;i < [self.dataarray count] ;i++)       {                 nsdictionary *datadic = [self.dataarray objectatindex:i];        nsmutabledictionary *projectdic = [[nsmutabledictionary alloc] init];       [projectdic setobject:[datadic objectforkeyedsubscript:projectid] forkey:common_id];       [projectdic setobject:[datadic objectforkeyedsubscript:project] forkey:common_name];        nsmutabledictionary *issuedic = [[nsmutabledictionary alloc] init];       [issuedic setobject:[datadic objectforkeyedsubscript:issueid] forkey:common_id];        nsmutabledictionary *activitydic = [[nsmutabledictionary alloc] init];       [activitydic setobject:[datadic objectforkeyedsubscript:activityid] forkey:common_id];       [activitydic setobject:[datadic objectforkeyedsubscript:activity] forkey:common_name];               (int j=0; j<7; j++) {                   wkday=@"";strhours =@"";                   wkday = self.wkdatearray[i];                  strhours = [nsstring stringwithformat:@"%@",[datadic objectforkeyedsubscript:wkday]];                   if(strhours.length)                   {                         [sdetails setobject:[datadic objectforkeyedsubscript:entryid] forkey:common_id];                         [sdetails setobject:projectdic forkey:project];                         [sdetails setobject:issuedic forkey:issue];                         [sdetails setobject:entryuser forkey:currentuser];                         [sdetails setobject:activitydic forkey:activity];                         [sdetails setobject:strhours forkey:@"hours"];                         [sdetails setobject:[datadic objectforkeyedsubscript:comment] forkey:comment];                         [sdetails setobject:wkday forkey:@"spent_on"];                         [time_entry addobject:sdetails];                                           }                    }       }        nsmutabledictionary *results = [[nsmutabledictionary alloc] init];       [results setobject:time_entry forkey:@"entries"];       [results setobject:entryuser forkey:currentuser];       [results setobject:@"2016-01-17" forkey:@"startday"];       [results setobject:@"new" forkey:@"status"];       [results setobject:@"0.0" forkey:@"total"];        nsmutabledictionary *wktime = [[nsmutabledictionary alloc] init];       [wktime setobject:results forkey:@"time"];    nsstring *jsonstr = [nsstring stringwithformat:@"%@",wktime];       nserror *jsonerror;       nsdata *requestdata = [jsonstr datausingencoding:nsutf8stringencoding];         nsdictionary *jsondata = [nsjsonserialization jsonobjectwithdata:requestdata options:nsjsonreadingmutablecontainers error:&jsonerror];        nslog(@"json data : %@",jsondata);       nslog(@"error values: %@",jsonerror); 

error domain=nscocoaerrordomain code=3840 "the operation couldn’t completed. (cocoa error 3840.)" (no value key in object around character 16.) userinfo=0x7feb58f21b90 {nsdebugdescription=no value key in object around character 16.}

as avi pointed out,

  nsstring *jsonstr = [nsstring stringwithformat:@"%@",wktime];   nsdata *requestdata = [jsonstr datausingencoding:nsutf8stringencoding];     nsdictionary *jsondata = [nsjsonserialization jsonobjectwithdata:requestdata options:nsjsonreadingmutablecontainers error:nil]; 

you have created string , converted nsdata, later trying use nsjson api convert dictionary, fail since original string isnt json encoded.

you should convert dict data using nsjson api , read back, although use wont make sense till tell context.

  nsdata *requestdata = [nsjsonserialization datawithjsonobject: wktime options:nsjsonwritingprettyprinted error:nil];    nsdictionary *jsondata = [nsjsonserialization jsonobjectwithdata:requestdata options:nsjsonreadingmutablecontainers error:nil]; 

if trying web request, suggest reading afnetworking docs, api built , can lot web requests.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -