Application Analysis of JSON in Android

  • 2021-07-26 08:55:23
  • OfStack

This paper analyzes the related applications of JSON 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. Mainstream technology in the industry provides a complete solution (somewhat similar to regular expressions, which is supported by most languages today), so that data can be exchanged between different platforms. JSON adopts a highly compatible text format, and it also has similar behavior to C language system. Json. org

Structure of JSON:

Name/Value Pairs is similar to the well-known Keyed list, Hash table, Disctionary and Associative array. There is another class "Bundle" in Android platform, which has similar behavior to some extent. org. json. JSONObject

Array, an ordered list of data. org. json. JSONArray

There are four classes related to JSON and one Exceptions in Android:

JSONArray
JSONObject
JSONStringer
JSONTokener
JSONException

JSONObject:

This is the basic unit of JSON definition in the system, which contains one pair of Key/Value values. Its response to an external call (External: the numeric value that applies the output of the toString () method) is embodied as a standard string (for example: {"JSON": "Hello, World"}, outermost wrapped in curly braces, where Key and Value are separated by a colon ":"). Its operation format for internal (Internal) behavior is slightly, for example, initialize an JSONObject instance and add values by referring to the internal put () method: new JSONObject (). put ("JSON", "Hello, World! "), separated by commas", "between Key and Value.

The types of Value include: Boolean, JSONArray, JSONObject, Number, String, or the default value JSONObject. NULL object.

There are two different methods of taking values:

get (): Used if a certain 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, it will return 1 default value without throwing an exception.

JSONArray:

It represents a set of ordered values. Converting it to String output (toString) is in the form of 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). This class also has query behavior inside. get () and opt () can both return the specified value through the index index, and put () method is used to add or replace the value.

Similarly, the value type of this class can include: Boolean, JSONArray, JSONObject, Number, String, or the default value JSONObject. NULL object.

JSONStringer:

According to the official explanation, this class can help create JSONtext quickly and conveniently. Its greatest advantage is that it can reduce program exceptions caused by formatting errors. Referencing this class can automatically create JSON text strictly according to JSON syntax rules (syntaxrules). Only one JSON text can be created for each JSONStringer entity.

Follow the following example for other relevant information:


string myString = new JSONStringer().object()
                  .key("AR").value("www.Androidres.com!")
                  .endObject()
                  .toString();

The result is a set of standard format JSON text: {"AR": "www. Androidres. com!"}
Where. object () and. endObject () must be used together to add boundaries to values according to the Object standard. Similarly, there is a standard set of methods for arrays to generate bounds.array () and.endArray ().

JSONTokener:

This is the class that the system parses JSON source string for the JSONObject and JSONArray constructors, which extracts numeric information from source string.

JSONException:

Is the exception information thrown by the JSON. org class.

A complete application example is quoted below (from: androidsnippets. org)

Apply JSONObject to store Map type values:


public static JSONObject getJSON(Map map) {
  Iterator iter = map.entrySet().iterator();
  JSONObject holder = new JSONObject();
  while (iter.hasNext()) {
    Map.Entry pairs = (Map.Entry) iter.next();
    String key = (String) pairs.getKey();
    Map m = (Map) pairs.getValue();
    JSONObject data = new JSONObject();
    try {
      Iterator iter2 = m.entrySet().iterator();
      while (iter2.hasNext()) {
        Map.Entry pairs2 = (Map.Entry) iter2.next();
        data.put((String) pairs2.getKey(), (String) pairs2.getValue());
      }
      holder.put(key, data);
    } catch (JSONException e) {
      Log.e("Transforming", "There was an error packaging JSON",e);
    }
  }
  return holder;
}

For more readers interested in Android related contents, please check the topics on this site: "Summary of activity Operation Skills for Android Programming", "Summary of Android File Operation Skills", "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of View Skills for Android View" and "Summary of Android Control Usage"

I hope this paper is helpful to everyone's Android programming.


Related articles: