字典转json我们要注意NSJSONWritingOptions这个枚举类型:
typedef NS_OPTIONS(NSUInteger, NSJSONWritingOptions) {
NSJSONWritingPrettyPrinted = (1UL << 0),
/* Sorts dictionary keys for output using [NSLocale systemLocale]. Keys are compared using NSNumericSearch. The specific sorting method used is subject to change.
*/
NSJSONWritingSortedKeys API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) = (1UL << 1),
NSJSONWritingFragmentsAllowed = (1UL << 2),
NSJSONWritingWithoutEscapingSlashes API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0)) = (1UL << 3),
} API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
-
NSJSONWritingPrettyPrinted = (1UL << 0) //是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。(自己原生打印输出,一般选用这个可读性比较高);
-
NSJSONWritingSortedKeys //输出的json字符串就是一整行(如果要往后台传或者字典转json然后加密,就不能格式化,会有换行符和空格);这个枚举是iOS11后才出的,iOS11之前我们可以用kNilOptions来替代
-
NSJSONWritingFragmentsAllowed 允许写入片段
-
NSJSONWritingWithoutEscapingSlashes 不转义斜线
网友评论