Analysis of JSON-related applications in Android _android

Source: Internet
Author: User
Tags numeric value

This article analyzes the application of JSON in Android. Share to everyone for your reference, specific as follows:

The definition of JSON:

A lightweight data interchange format with good readability and fast-coding features. The industry's mainstream technology provides it with a complete solution (a bit like regular expressions that are supported by most languages today), allowing data exchange between platforms. JSON uses a highly compatible text format, as well as behavior similar to the C language system. –json.org

The structure of JSON:

Name/value pairs, similar to the familiar keyed list, Hash table, disctionary, and associative array. There is another class "Bundle" in the Android platform that has similar behavior in some ways. Org.json.JSONObject

Array, a set of ordered lists of data. Org.json.JSONArray

Contains four JSON-related classes and a exceptions in Android:

Jsonarray
Jsonobject
Jsonstringer
Jsontokener
Jsonexception

Jsonobject:

This is the basic unit of the system for the JSON definition, which contains a pair of key/value values. Its response to an external (External: The value of the output of the application ToString () method) is reflected as a standard string (for example: {"JSON": "Hello, World"}, wrapped in curly braces, where key and value are separated by a colon ":"). It has a slight operational format for internal (Internal) behavior, such as initializing a Jsonobject instance, and referencing the internal put () method to add a value: New Jsonobject (). Put ("JSON", "Hello, world!"), The key and value are separated by commas ",".

The types of value include: Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.

There are two different methods of taking values:

Get (): used under conditions that determine the existence of a value, or a exception message will be thrown if the associated key cannot be retrieved.
Opt (): This method is relatively flexible and will return a default value when the specified value cannot be obtained, and will not throw an exception.

Jsonarray:

It represents a set of ordered values. The form of converting it to string output (toString) is wrapped in square brackets, with values separated by commas "," (for example, [Value1,value2,value3], where you can use the short code to get a more intuitive understanding of its format). The interior of this class also has query behavior, and both the Get () and opt () methods can return the specified value through the index index, and the put () method is used to add or replace the numeric value.

The value type of the same class can include Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.

Jsonstringer:

According to the official explanation, this class can help to create jsontext quickly and easily. The biggest advantage of this is that you can reduce the error in the format resulting in a program exception, referencing this class can automatically create JSON text strictly according to the JSON syntax rules (syntaxrules). Only one JSON text can be created for each Jsonstringer entity.

Follow the example below to find out about other information:

String myString = new Jsonstringer (). Object ().
                  Key ("AR"). Value ("www.androidres.com!")
                  . EndObject ()
                  . toString ();

The result is a standard set of JSON-formatted text:{"AR": "www.androidres.com!"}
The. Object () and. EndObject () must be used at the same time to add boundaries to the values according to the object criteria. Similarly, there is a standard set of methods for arrays to generate boundaries. Array () and. Endarray ().

Jsontokener:

This is the class that the system parses the JSON source string for the Jsonobject and Jsonarray constructors, and it can extract numeric information from the source string.

Jsonexception:

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

The following refers to a complete application instance (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 is an error packaging JSON", e);
    }
  return holder;
}

More interested readers of Android content can view this site: "The activity of Android programming skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Development Primer and Advanced tutorials, Android Resource operations tips, Android View overview and Android Control Usage summary

I hope this article will help you with the Android program.

Related Article

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.