我收到JSON回应

(
        {
        Response = success;
        UserId = 287;
    }
)


我试图用下面的代码解析用户ID

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&e];
    NSLog(@"%@", dataDictionary);
    NSString *useridstring=[dataDictionary valueForKey:@"UserId"];


但是正在获取用户ID ( 287 )
请建议我如何获取不带括号的用户ID

最佳答案

您的回答不是字典,而是数组。因此,您的代码应如下所示:

NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&e];
NSLog(@"%@", dataArray);
NSDictionary *dataDictionary = dataArray[0]; //Same as objectAtIndex:
NSString *userIDString = dataDictionary[@"UserID"]; //Same as objectForKey:
//If you're still getting parens try this:
//int userIDInt = [dataDictionary[@"UserID"] intValue];

08-19 05:28