我的JSON字符串是
"{\"CommentList\":[{\"SubmittedDate\":\"02/01/2017 06:09:32\",\"SubmittedTime\":\"\",\"UserId\":\"af0e1cda5c0\",\"UserName\":\"NepolionBon\",\"Comments\":\"\",\"Complete_Hour\":\"\",\"Complete_Minute\":\"\"}]}"
我需要" SubmittedDate"和" Complete_Hour"从它。
当我尝试使用
转换此字符串时NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
收到错误
-[__NSSingleObjectArrayI dataUsingEncoding:]: unrecognized selector sent to instance 0x14decfc0
任何人都可以帮助我吗?
答案 0 :(得分:0)
您可以使用此代码获取SubmittedDate
和Complete_Hour
。
NSString *jsonString = @"{\"CommentList\":[{\"SubmittedDate\":\"02/01/2017 06:09:32\",\"SubmittedTime\":\"\",\"UserId\":\"af0e1cda5c0\",\"UserName\":\"NepolionBon\",\"Comments\":\"\",\"Complete_Hour\":\"\",\"Complete_Minute\":\"\"}]}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray* arrComments = [json objectForKey:@"CommentList"];
for (NSDictionary *dict in arrComments) {
NSLog(@"Submit date :%@",[dict objectForKey:@"SubmittedDate"]);
NSLog(@"Comlplete Hour : %@",[dict objectForKey:@"Complete_Hour"]);
}