我有一个dictionary
我需要生成一个JSON string
通过使用dictionary
。可以转换吗?你们能帮忙解决这个问题吗?
Apple 在 iOS 5.0 和 Mac OS X 10.7 中添加了 JSON 解析器和序列化器。看NSJSON序列化 https://developer.apple.com/documentation/foundation/nsjsonserialization.
要从 NSDictionary 或 NSArray 生成 JSON 字符串,您不再需要导入任何第三方框架。
操作方法如下:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToOutput
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)