Ios-xml&json parsing

Source: Internet
Author: User

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); 










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.