[英]The operation couldn’t be completed. (Cocoa error 3840.)" (No value for key in object around character 16.)
Below is my 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];
for (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 be completed. (Cocoa error 3840.)" (No value for key in object around character 16.) UserInfo=0x7feb58f21b90 {NSDebugDescription=No value for key in object around character 16.}
错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840。)”(字符16周围的对象的键没有值。)UserInfo = 0x7feb58f21b90 {NSDebugDescription =字符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 a string and converted it into NSData
, but later trying to use the NSJSON
API to convert it into a dictionary, which will fail since the original string isnt json encoded. 您已经创建了一个字符串并将其转换为
NSData
,但是稍后尝试使用NSJSON
API将其转换为字典,由于原始字符串未经过json编码,因此该方法将失败。
You should just convert the dict into Data using the NSJSON
API and just read back, although its use wont make much sense till you tell the context. 您应该只使用
NSJSON
API将dict转换为Data并回读,尽管在您告诉上下文之前它的用法没有多大意义。
NSData *requestData = [NSJSONSerialization dataWithJSONObject: wktime options:NSJSONWritingPrettyPrinted error:nil];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingMutableContainers error:nil];
If you are trying to do a web request, i would suggest reading AFNetworking docs, that API is well built and can help a lot for web requests. 如果您尝试执行Web请求,我建议您阅读AFNetworking文档,该API的构建良好,可以为Web请求提供很多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.