This article analyzes the JSON-related applications in Android. Share it for your reference, as follows:
Definition of JSON:
A lightweight data exchange format with good readability and easy to write quickly. The mainstream technology in the industry provides it with a complete solution (somewhat similar to regular expressions, supported by most languages today), allowing data exchange between different platforms. JSON adopts a highly compatible text format and also has behaviors similar to the C language system. –
The structure of JSON:
Name/Value Pairs, similar to the well-known Keyed list, Hash table, Disctionary and Associative array. There is another class "Bundle" in the Android platform at the same time, which has similar behavior to some extent.
Array, an ordered list of data.
Includes four JSON-related classes and one Exceptions in Android:
JSONArray
JSONObject
JSONStringer
JSONTokener
JSONException
JSONObject:
This is the basic unit of the JSON definition in the system, which contains a pair of (Key/Value) values. Its response to an external (External: the value output by applying the toString() method) is reflected as a standard string (for example: {"JSON": "Hello, World"}, with the outermost wrapped in braces, and the Key and Value are separated by colons":"). Its operation format for internal (Internal) behavior is slightly different. For example, initialize a JSONObject instance, refer to the internal put() method to add a value: new JSONObject().put("JSON", "Hello, World!"), separated by commas" and "" between Key and Value.
The types of Value include: Boolean, JSONArray, JSONObject, Number, String, or default object.
There are two different values:
get(): Used under the condition that the numeric value exists, otherwise an Exception message will be thrown when the relevant key cannot be retrieved.
opt(): This method is relatively flexible. When the specified value cannot be obtained, a default value will be returned and no exception will be thrown.
JSONArray:
It represents an ordered set of numerical values. The form of converting it to String output (toString) is wrapped in square brackets, and the values are separated by commas "," (for example: [value1, value2, value3], you can use short code to understand its format more intuitively). The inside of this class also has query behavior. Both get() and opt() methods can return the specified value through the index index, and the put() method is used to add or replace the value.
The value types of this class can also include: Boolean, JSONArray, JSONObject, Number, String or default object.
JSONStringer:
According to the official explanation, this class can help create JSONtext quickly and easily. Its biggest advantage is that it can reduce program exceptions due to format errors. Referring to this class can automatically create JSON text strictly in accordance with the JSON syntax rules (syntaxrules). Each JSONStringer entity can only create one JSON text corresponding to it.
Learn more about other related information based on the following example:
string myString = new JSONStringer().object() .key("AR").value("!") .endObject() .toString();
The result is a set of JSON text in standard format: {"AR":"!"}
The .object() and .endObject() must be used at the same time to add boundaries to the numerical values according to the Object standard. Similarly, there is a standard set of methods for generating boundaries.array() and .endArray() for arrays.
JSONTokener:
This is the class that the system parses JSON source string for JSONObject and JSONArray constructors, which can extract numerical information from the source string.
JSONException:
is the exception information thrown by the class.
The following is a complete application example (from:)
Apply JSONObject to store Map type values:
public static JSONObject getJSON(Map map) { Iterator iter = ().iterator(); JSONObject holder = new JSONObject(); while (()) { pairs = () (); String key = (String) (); Map m = (Map) (); JSONObject data = new JSONObject(); try { Iterator iter2 = ().iterator(); while (()) { pairs2 = () (); ((String) (), (String) ()); } (key, data); } catch (JSONException e) { ("Transforming", "There was an error packaging JSON",e); } } return holder; }
For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.