不,不是的。看这个例子:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
nil];
NSLog(@"dict=%@", dict);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSString *jsonAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *jsonDataFromString = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];
// DO NOT DO THIS:
// jsonDataFromString = [jsonDataFromString subdataWithRange:NSMakeRange(0, [jsonDataFromString length] - 1)];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonDataFromString options:0 error:nil];
NSLog(@"jsonObject=%@", jsonObject);
尝试一下,然后在未注释的“不要这样做”行中尝试。你会看到没有问题。