One. XML parsing
1. Obtain the network data and place it in the Xmlparser parser.
NSString *[email protected] "http://api.zbw.vc/api/MobileDesktop/GetUpdateAdvertisinToMobileIndex?ct=xml"; Nsurl *url=[nsurl Urlwithstring:urlstr]; Nsxmlparser *parser=[[nsxmlparser alloc] initwithcontentsofurl:url];
2. Set up the resolution agent.
[Parser setdelegate:self];
3. Start parsing.
[Parser parse];
4. The agent defined above is itself. That is, the agent is<NSXMLParserDelegate>
It generally must go the following way: (similar to Android Xmlparser parsing)
#prag mark-Start parsing tags, multiple calls-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: ( NSString *) NamespaceURI qualifiedname: (NSString *) QName attributes: (nsdictionary *) Attributedict#prag mark-Parse element, Multiple invocations-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) String#prag mark-Parse element label, multiple calls-(void) parser: ( Nsxmlparser *) Parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: ( NSString *) QName attributes: (nsdictionary *) Attributedict#prag mark-Parse Error-(void) Parser: (Nsxmlparser *) parser Validationerroroccurred: (Nserror *) Validationerror#prag mark-Starts parsing the document, 1 times-(void) Parserdidstartdocument: (Nsxmlparser *) Parser#prag mark-Parse end of document, 1 times-(void) Parserdidenddocument: (Nsxmlparser *) parser
Two. JSON parsing
JSON parsing can be done by The class method of the Nsjsonserialization class to parse, as follows
Get network data nsurl *url=[nsurl urlwithstring:@ "Http://api.zbw.vc/api/MobileDesktop/GetUpdateAdvertisinToMobileIndex? Ct=json "]; NSData *data=[nsdata Datawithcontentsofurl:url]; Parse by JSON to array Nsarray *array=[nsjsonserialization Jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; Data storage Nsarray *pathdir=nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsalldomainsmask, YES); NSString *path=[pathdir[0] stringbyappendingpathcomponent:@ "Ad.plist"];//NSLog (@ "%@", array[0]); [Array Writetofile:path atomically:yes]; Take out the data and print Nsmutablearray *arr2=[nsmutablearray array]; For (nsdictionary *dic in array) {Cszwallpaper *wallpaper=[[cszwallpaper alloc] init]; Wallpaper.imageurl=[dic objectforkey:@ "ImageUrl"]; wallpaper.isupdate=[((NSNumber *) [dic objectforkey:@ "Isupdate"]) IntegerValue]; wallpaper.type=[((NSNumber *) [dic objectforkey:@ "type"]) IntegerValue]; [Arr2 Addobject:wallpaper]; } NSLog (@ "parsed object: >>%@", arr2);