The JSON data format is widely used in Android for client-to-Network (or server) communication. It is very necessary for you to learn about it systematically.
I have recently made a simple study on json. I would like to summarize it to everyone.
In order to make the article concise and clear, try to add as many columns as possible, with less nonsense.
Reference: http://www.ietf.org/rfc/rfc4627.txt? Number = 46271. JSON Parsing
(1). parse one of the objects:
Copy code
Resolution method:
Copy code
(2). parse Object 2:
Copy code
Resolution method:
Copy code
(3). parse one of the arrays:
Copy code
Resolution method:
Copy code
(4). parse Array 2:
Copy code
Resolution method:
Copy code
(5). parse Object and Array:
Copy code
Resolution method:
Copy code
Therefore, we find that get is followed by the type of the expected result: getType, which is helpful for understanding.
(6). Use optType:
In the preceding example, when getType is used, an exception is thrown when the node cannot be found.
If optType is used and no node is found, null or default value is returned.
Copy code
(7). UTF-8 BOM header causes JSON parsing exceptions
When the json file is saved as UTF-8, the bom Header "ef bb ef" is generated at the beginning of the text (which can be viewed only when it is opened using a hexadecimal tool ).
There are two solutions:
A. Use UltraEdit to open the json file, save as, select the format of UTF-8, no BOM header, if not, open in notepad, save as UTF-8, try multiple times on it.
B. Use code processing to intercept the json body content:
Copy code
2. JSON is required
(1). JSON is a lightweight data exchange format.
(2) JSON is based on two data structures: Object and Array. The Object is a set of "name/value" pairs.
(3). Object: braces. Each string-value group is separated by ",", and string and value are separated by colons.
<Ignore_js_op>
(4). array:
<Ignore_js_op>
(5) string is a set of any number of Unicode characters enclosed by double quotation marks, which are escaped using a backslash.
<Ignore_js_op>
(6). value can be a string, number, true, false, null, object, or array enclosed in double quotes ). These structures can be nested.
<Ignore_js_op>
(7). The blank space can be added to any symbol, including space, tab, carriage return, and line feed.
(8). Example:
A. Object instance:
Copy code
B. Array instance:
Copy code
It is very simple. It is very basic. Only when the water accumulation reaches the river and the bricks are used to build a building.