Android Learning Notes-seventh chapter data parsing and Reading

Source: Internet
Author: User

Seventh chapter data Analysis and Reading

  1. How data is organized in network transport: XML, JSON, HTTP

    (1) XML: Extensible Markup Language

    Features: Save a small amount of data, cross-platform

    Parsing methods: Sax, DOM, pull

    A. Sax parsing:

    Features: Fast speed, low memory consumption. Event-driven and does not load all content into memory at once.

    Startelement: Element node start

    LocalName: Label name without a prefix

    QName: Tagged name with prefix

    Attributes: Attributes and values within a node

    <student id = "1" >


    <name> Xiao Ming </name>

    </student>

    (2) Dom parsing:

    Feature: Load all content into memory at once

    Use for server-side programming

    (3) Pull analysis:

    Features: Similar to sax, does not automatically read

    For mobile devices with low memory consumption

  2. JSON parsing

    Features: More space-saving relative to XML

    (1) JSON object: {"Key1": "Value1", "Key2": "Value2"}

    A. Start and end with {}

    B. Key must be a string, value can be the underlying data type, string, JSON object, JSON array

    C. comma-delimited data for each piece

    Example:

    {"Name": "Xiaoming", "Age": "Married": "True"}

    (2) parsing of JSON

    Gets the Jsonobject type object based on the string passed in

    Jsonobject jsonobject = new Jsonobject (str);

    According to key, get the corresponding data

    String name = jsonobject.getstring ("name");

    int age = Jsonobject.getint ("Age");

    Boolean B = Jsonobject.getboolean ("Married")


    To pass in data to Jsononject

    Jsonobject.put ("name", "Little Red");

    Jsonobject.put ("Age", 20);

    Jsonobject.put ("Married", "false");

    (3) Jsonarray JSON array

    A. Start and end with []

    B. Each piece of data is a jsonobject

    C. comma-delimited data for each piece

    Example:

    [{"Name": "Xiaoming", "age": +, "sex": "Male"},{"name": "Little Red", "age": All, "sex": "Female"}]

    Parse data in a Jsonarray format

    Gets an object of type Jsonarray based on the string passed in

    Jsonarray Jsonarray = new Jsonarray (str);

    for (int i=0; i<jsonarray.length;i++) {

    Get the Jsonobject object according to subscript

    Jsonobject Jsonobject = Jsonarray.getjsonobject (i);

    Get the corresponding data based on the key value

    String name = jsonobject.getstring ("name");

    int age = Jsonobject.getint ("Age");

    String sex = jsonobject.getstring ("Sex");

    }


    To pass in data to Jsonarray

    Jsonarray Jsonarray = new Jsonarray ();

    Jsonobject JsonObject1 = new Jsonobject ();

    Jsonobject jsonObject2 = new Jsonobject ();

    Jsonobject1.put ("name", "small black");

    Jsonobject2.put ("name", "small white");

    Jsonarray.put (JSONOBJECT1);

    Jsonarray.put (JSONOBJECT2);

Android Learning Notes-seventh chapter data parsing and Reading

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.