- [__ NSSingleObjectArrayI dataUsingEncoding:]目标C.

时间:2017-02-06 07:14:53

标签: ios objective-c json nsarray

我的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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用此代码获取SubmittedDateComplete_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"]);

    }