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