下面是我的示例代码
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.}
最佳答案
正如 Avi 指出的那样,
NSString *jsonStr = [NSString stringWithFormat:@"%@",wktime];
NSData *requestData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingMutableContainers error:nil];
您已经创建了一个字符串并将其转换为 NSData
,但稍后尝试使用 NSJSON
API 将其转换为字典,这将失败,因为原始字符串不是 json 编码的。
您应该使用 NSJSON
API 将 dict 转换为 Data 然后再读回,尽管在您告知上下文之前它的使用不会有多大意义。
NSData *requestData = [NSJSONSerialization dataWithJSONObject: wktime options:NSJSONWritingPrettyPrinted error:nil];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingMutableContainers error:nil];
如果您正在尝试执行 Web 请求,我建议您阅读 AFNetworking 文档,该 API 构建良好,可以为 Web 请求提供很多帮助。
关于ios - 操作无法完成。 ( cocoa 错误 3840。)”(字符 16 周围对象中的键没有值。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35038482/