Nsdictionary or Nsarray (nsmutablearray) ==>> nsdata ==>> nsstring (Json):

Source: Internet
Author: User
Tags parse error

The following code has a detailed comment explaining the conversions between these types (if there are errors in the place also look at the criticism pointed out, thank you!) )
-(void) viewdidload {[Super viewdidload]; /** * nsdictionary | | Nsarray (Nsmutablearray) ==>> nsdata ==>> nsstring (Json): *///nsdictionary *params = [NSDictionary dic tionarywithobjectsandkeys://@ "balance", @ "key",//@ "remaining Balan        Ce ", @" label ",//@" ", @" value ",//@" USD "@" CurrencyCode ", nil];                        Nsarray *params = @[@ "balance", @ "key", @ "remaining balance", @ "label",    @ "", @ "value" @ "USD" @ "CurrencyCode"];    Nserror *error = nil; NSData *jsondata = [nsjsonserialization datawithjsonobject:params options:nsjsonwritingprettyprinted error:&    ERROR];    NSString *jsonstring; if ([jsondata length] > 0 && error = nil) {jsonstring = [[NSString alloc] Initwithdata:jsondata Encodin        G:nsutf8stringencoding];    NSLog (@ "%@", jsonstring);  }else{      NSLog (@ "erroe:%@", error); }/** * NSString (Json): ==>>nsdata ==>> nsdictionary | | Nsarray (Nsmutablearray) *//convert NSString to nsdata nsdata *jsondata2 = [jsonstring datausingencoding:nsutf8stringenc     Oding];    Nserror *error2 = nil;    ID jsonobject = [nsjsonserialization jsonobjectwithdata:jsondata2 options:kniloptions Error:nil];    if (jsonobject! = Nil && Error2 = = nil) {NSLog (@ "Jsonobject = =%@", jsonobject);    }else{//Parse error NSLog (@ "erroe:%@", Error2); }/** NSData, NSString:returns:NSString *///nsstring *tempstr = [[NSString alloc] I            nitwithdata:<# (NSData *) #> encoding:<# (nsstringencoding) #>]; /** nsstring, NSData *///nsdata *tempdata = [[[NSString Alloc]init] datausingencoding:<# (NSStringEncod ing) #>]}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nsdictionary or Nsarray (nsmutablearray) ==>> nsdata ==>> nsstring (Json):

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.