1#import"ViewController.h"2 3 @interface Viewcontroller ()4 5 @end6 7 @implementation Viewcontroller8 9-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)EventTen { One [self test]; A } - --(void) Jsontooc the { - //1. Determine the URL -Nsurl *url = [Nsurl urlwithstring:@"Http://120.25.226.186:32812/login?username=123&pwd=456&type=JSON"]; - + //2. Create a Request object -Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; + A //3. Sending an asynchronous request at[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) { - //data----> is essentially a JSON string - //4. Parsing Data - //NSLog (@ "%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]); - - //JSON--->oc object deserialization in /* - first parameter: JSON binary data to a second parameter: + Third parameter: Error message - */ the /* * nsjsonreadingmutablecontainers = (1UL << 0), variable dictionary and array $ nsjsonreadingmutableleaves = (1UL << 1), all the strings inside are variable iOS7 and then the problem is not normallyPanax Notoginseng nsjsonreadingallowfragments = (1UL << 2) is neither a dictionary nor an array, you must use the enumeration value - */ the +NSString *STRM =@"\"wendingding\""; A the //nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:kniloptions Error:nil]; + -ID obj =[nsjsonserialization jsonobjectwithdata:[strm datausingencoding:nsutf8stringencoding] Options: Nsjsonreadingallowfragments Error:nil]; $ $NSLog (@"%@---%@", [objclass],obj); - - }]; the - }Wuyi the //JSON--->oc --(void) Jsonwithoc Wu { - //nsstring *STRM = @ "{\" error\ ": \" User name does not exist \ "}"; About //nsstring *STRM = @ "[\" Error\ ", \" user name does not exist \ "]"; $ //nsstring *strm = @ "\" wendingding\ ""; - //nsstring *STRM = @ "false"; - //nsstring *STRM = @ "true"; -NSString *STRM =@"NULL"; A +ID obj = [nsjsonserialization jsonobjectwithdata:[strm datausingencoding:nsutf8stringencoding] Options: Nsjsonreadingallowfragments Error:0]; theNSLog (@"%@---%@", [objclass],obj); - $ /* the josn OC the {} @{} the [] @[] the "" @"" - false NSNumber 0 in true NSNumber 1 the null nsnull is empty the */ About the //Nil the[NSNullNULL];//the method obtains a single grain, which is represented as null, and can be used in a dictionary or an array the + } - the //OC--->jsonBayi-(void) Octojson the { theNsdictionary *DICTM = @{ - @"name":@"dasheng11", - @" Age":@3 the }; the theNsarray *arraym = @[@"123",@"456"]; the - //Note: Not all OC objects can be converted to JSON the /* the -the outermost layer must be nsarray or nsdictionary the -All elements must be nsstring, NSNumber, Nsarray, nsdictionary, or NSNull94 -All keys in the dictionary must be of type nsstrings the -Nsnumbers can not die infinitely big the */ theNSString *STRM =@"wenidngding";98 AboutBOOL IsValid =[Nsjsonserialization ISVALIDJSONOBJECT:STRM]; - if(!isValid) {101NSLog (@"%zd", isValid);102 return;103 }104 the //OC--->json106 /*107 first parameter: OC object to convert108 second parameter: option nsjsonwritingprettyprinted typography109 */ theNSData *data =[nsjsonserialization datawithjsonobject:strm options:nsjsonwritingprettyprinted Error:nil];111 theNSLog (@"%@", [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]);113 } the the-(void) Test the {117Nsarray *arraym = [Nsarray arraywithcontentsoffile:@"/users/xiaomage/desktop/Classroom sharing/11 Great God class information/05-multi-threaded network/0225/data/apps.plist"];118NSLog (@"%@", Arraym);119 - //[Arraym writetofile:@ "/users/xiaomage/desktop/123.json" atomically:yes];121 122 //OC--->json123NSData *data = [nsjsonserialization datawithjsonobject:arraym options:nsjsonwritingprettyprinted Error:0];124[Data writetofile:@"/users/xiaomage/desktop/123.json"Atomically:yes]; the}
Ios-oc-json parsing-Nsjsonserialization