The interaction between the IOS dictionary and the string

Source: Internet
Author: User

/*!

* @brief Convert formatted JSON -formatted strings into dictionaries

* @param jsonstring JSON -formatted string

* @return return dictionary

*/

-(nsdictionary *) 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 @brief dictionary to string

* @param dic dictionary

* @return return string

*/

-(nsstring*) Dictionarytojson: (nsdictionary *) dic

{

nserror *parseerror = nil;

NSData *jsondata = [nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:& ParseError];

return [[NSString Alloc] Initwithdata:jsondata encoding:nsutf8stringencoding];

}


This article is from the "11204872" blog, please be sure to keep this source http://11214872.blog.51cto.com/11204872/1743144

The interaction between the IOS dictionary and the string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.