OC, oc Language

Source: Internet
Author: User

OC, oc Language

# Data Interaction format

  • The data that the server returns to the user is usually in the following two ways:
    • JSON
    • XML
  • JSON
    • A lightweight data format that is smaller than XML and is usually used by the server to return data to the mobile terminal.
    • When using data in a JSON file, you need to parse it. Apple's NSJSONSerialization has the best resolution performance.
    • JSON File Parsing in iOS converts it to an OC object, as follows:
      • Braces {} --> dictionary NSDictionary
      • Brackets [] --> array NSArray
      • Double quotation marks "" --> string NSString
      • Number --> NSNumber
    • JSON is usually parsed using NSJSONSerialization, with the best performance
  • XML
    • XML (Extension Markup Language), eXtensible Markup Language
    • Is a common data format used by the server to return to the client.
    • XML documents, usually including the following content
      • Document declaration
      • Element)
      • Attribute)
    • XML document parsing Scheme
      • Small file: NSXMLParser, native
      • Large file: GDataXML, developed by Google, based on libxml2

# JSON Parsing

  • Resolution type

    • NSDictionary and NSArray data are parsed by default.
    • To parse NSString data, use the NSJSONReadingAllowFragments parameter.
  • An object must follow the following rules to be converted to a JSON document.

    • The root node is of the NSDictionary or NSArray type.
    • All objects must be one of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
    • Limited number of objects
  • Conversion of JSON data and OC objects

    • JSON --> OC
    + (Nullable id) JSONObjectWithData :( NSData *) data options :( NSJSONReadingOptions) opt error :( NSError **) error/*** data: NSData type data to be parsed * opt: resolution Method * error: error message */
    • OC --> Json
    + (Nullable NSData *) dataWithJSONObject :( id) obj options :( NSJSONWritingOptions) opt error :( NSError **) error/*** obj: The OC object to be converted * opt: resolution Method * error: error message */
  • Parse NSString

    NSString * json = "I am a coder"; [NSJSONSerialization JSONObjectWithData: [json dataUsingEncoding: NSUTF8StringEncoding] options: NSJSONReadingAllowFragments error: nil]/*** JSON-parsed data is generally NSData type data encoded using NSUTF8StringEncoding * options: specifies the type that can be parsed. NSJSONReadingAllowFragments indicates that NSString objects can be parsed * error: return resolution error message * return value: resolved NSString object */
  • Parse NSDictionary and NSArray (data from the server)

    /** Block-based request sending * // create a request object NSURLRequest * request = [NSURLRequest requestWithURL :( this is the request Path)]; // send a request [NSURLConnection sendAsynchronousRequest: request queue: [[delealloc] init] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: nil];}];/*** queue: queue of the request Task * completionHandler: Block for callback completion * response: Server response Information * data: the data returned by the server * error: error message * parses the data into the corresponding NSDictionary object */

# XML Parsing

  • Small File Parsing (NSXMLParser)

    • To parse XML documents through NSXMLParser, you need to use the NSXMLParserDelegate protocol.
    • Common NSXMLParserDelegate protocols
    // 1. call-(void) parserDidStartDocument :( NSXMLParser *) parser when parsing the XML document. // 2. calls when parsing an element in the XML document at the beginning, parsing the core of the task-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( nullable NSString *) namespaceURI qualifiedName :( nullable NSString *) qName attributes :( NSDictionary <NSString *, NSString *> *) attributeDict/*** elementName: name of the element being parsed * attributeDict: parse the generated OC object * // 3. call-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( nullable NSString *) namespaceURI qualifiedName :( nullable NSString *) qName // 4. call-(void) parserDidEndDocument :( NSXMLParser *) parser when parsing to the end of the XML document
    • Resolution steps
    // 1. create a parser NSXMLParser * parser = [[NSXMLParser alloc] initWithData: data]; // 2. sets the proxy parser. delegate = self; // 3. start the parser [parser parse]; // 4. complete the specific parsing task in the proxy method
  • Big File Parsing (GDataXML)

    • Several Classes related to GDataXML
      • GDataXMLDocument: The entire XML document
      • GDataXMLElement, an element in the XML document
    • Resolution steps
    // 1. convert XML document data to GDataXMLDocument type object GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData: data options: 0 error: nil]; // 2. obtain all the elements of the root node in the GDataXMLDocument object. NSArray * elements = [document. rootElement elementsForName: @ "here is the name of the element"];/*** get all elements with the specified name in the XML document * // 3. parse the data in each element for (GDataXMLElement * ele in elements) {[ele attributeForName: @ "here is the Key in the element"]. stringValue;/*** parse the attribute value of the Key in the element ele, and convert it into a string object */}

     

Related Article

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.