JSON conversion issues are often encountered during development. Take a note of your own code below:
#pragma mark-json Operation/** * Convert formatted JSON-formatted string into dictionary * * @param jsonstring jsonstring JSON-formatted string * * @return return dictionary */+ (nsdic Tionary *) dictionarywithjsonstring: (NSString *) jsonstring{if (jsonstring = nil) {return nil; } nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding]; Nserror *err; Nsdictionary *dic = [Nsjsonserialization jsonobjectwithdata:jsondata Options:nsjsonreadingmutablecontainers error:&err]; if (err) {NSLog (@ "JSON parse failed:%@", err); return nil; } return dic;} /** * Convert a JSON-formatted object (dictionary) into a JSON string * * @param jsonobject JSON Object * * @return a JSON-formatted string */+ (NSString *) Jsonstringwithobjec T: (ID) jsonobject{//Convert dictionary or array to JSON string nserror *error = nil; NSData *jsondata = [Nsjsonserialization datawithjsonobject:jsonobject Options:nsjsonwritingprettyprinTed error:&error]; NSString *jsonstring = [[NSString alloc] Initwithdata:jsondata encoding:ns Utf8stringencoding]; if ([jsonstring length] > 0 && error = = nil) {return jsonstring; }else{return nil; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ios_json string to Dictionary, dictionary to JSON string