我是 iOS 新手。我创建了一个JSON NSDictionary
像这样:
NSArray *keys = [NSArray arrayWithObjects:@"User", @"Password", nil];
NSArray *objects = [NSArray arrayWithObjects:@"Ali", @"2020", nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
然后我可以将它转换为NSString
通过两种机制:
1)
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
NSString *jsonString = nil;
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
2)
NSString *jsonString = [jsonDictionary JSONRepresentation];
通过第二种方式我得到了这个warning
:
Instance method '-JSONRepresentation' not found (return type defaults to 'id')
但是当我运行该项目时,两种机制都工作正常:
NSLog(@"Val of json parse obj is %@",jsonString);
您知道如何通过第二种方式消除警告吗?
我的主要目标是POST
使用 RESTful Web 服务将此 json 字符串传输到外部数据库。
考虑到我的主要目标,基本上哪种方法更好?