Dictionary string to JSON
+ (nsstring*) Dictionarytojson: (Nsdictionary *) dic
{
Nserror *parseerror = nil;
NSData *jsondata = [nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:& ParseError];
return [[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding];
}
In fact, all data types can be processed with the above model because
+ (Nullable NSData *) Datawithjsonobject: (ID) obj options: (nsjsonwritingoptions) opt error: (NSERROR *) error;
The class of object in this NSData class method is of type ID, so other types can be converted to NSData type data before being converted to a string or dictionary as needed.
Convert to JSON string:
[[NSString Alloc] Initwithdata:jsondata encoding:nsutf8stringencoding];
In general, as long as the conversion to a JSON string is OK, the server will parse
Convert JSON string to dictionary
NSData *jsondata = [jsonstring datausingencoding:nsutf8stringencoding];
Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers Error: NIL];
iOS different data types are converted to JSON type