Android Gson using--json2 objects and objects 2json

Source: Internet
Author: User

The JSON (JavaScript Object Notation) is a lightweight data interchange format that uses a completely language-independent text format, providing an ideal data Interchange Format for Web application development.

 

In fact, to create and parse JSON data, you can also use Gson to do so. Gson is a Java class library provided by Google for mapping between Java objects and JSON data. With Gson, you can easily convert a string of JSON data into a Java object, or convert a Java object to the appropriate JSON data.

Two important methods of 1.GSON

In the Gson API, two important methods are available: the ToJson () and the Fromjson () methods. where the ToJson () method is used to convert the Java object to the appropriate JSON data, the Fromjson () method is used to implement the conversion of the JSON data to the appropriate Java object.

1.1 ToJson () method

The ToJson () method is used to convert a Java object to the appropriate JSON data, mainly in the following ways:

(1) String ToJson (jsonelement jsonelement);

(2) String ToJson (Object src);

(3) String ToJson (Object src, Type typeofsrc);

Wherein, method (1) is used to convert the Jsonelement object (can be jsonobject, Jsonarray, etc.) into JSON data; method (2) is used to serialize the specified object object into the corresponding JSON data Method (3) is used to serialize the specified object object (which can include a generic type) into the corresponding JSON data.

1.2 Fromjson () method

The Fromjson () method is used to convert JSON data to the appropriate Java object, mainly in the following ways:

(1) <T> T Fromjson (jsonelement json, class<t> Classoft);

(2) <T> T Fromjson (jsonelement json, Type Typeoft);

(3) <T> T Fromjson (Jsonreader Reader, Type Typeoft);

(4) <T> T Fromjson (Reader Reader, class<t> Classoft);

(5) <T> T Fromjson (Reader Reader, Type Typeoft);

(6) <T> T Fromjson (String json, class<t> Classoft);

(7) <T> T Fromjson (String json, Type Typeoft);

The methods above are used to parse different forms of JSON data into Java objects.

2. Generating JSON data on the server side

 

public class Jsontools {

/*

* Function: Generate JSON string

* Param:value Object objects that you want to convert to a JSON string

* Retuen:json String

* Author: Blog Park-still indifferent

*/

public static String createjsonstring (Object value) {

Gson Gson = new Gson ();

String string = Gson.tojson (value);

return string;

}

}


650) this.width=650; "Src=" http://images.cnitblog.com/blog/430074/201305/ 19231525-563945ed29934cf88f15ab9ccbd00977.jpg "style=" margin:0px auto;padding:0px;border:0px;/>

Figure 1 The resulting JSON data

3. Parsing JSON data on the client

In Android engineering, we can access the URL shown in Figure 1 through the HttpURLConnection interface to get JSON data on the server.

Once you have the JSON data, you can restore the JSON data shown in Figure 1 to the corresponding Person object list by using the previously mentioned Fromjson () method. Of course, because Gson is used here, you also need to import the Gson-2.2.1.jar package into the Android project. The specific implementation method is as follows.

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>

/*     * function  :    parse JSON data, revert to Person object list       * Param     :   jsonString           JSON data obtained from the server      * retuen    :    person Object List      */    public static  List<person> getlistperson (string jsonstring)  {         List<Person> list = new ArrayList<Person> ();         gson gson = new gson ();         list = gson.fromjson (jsonstring, new typetoken<list<person>> ()                    {}. GetType ()); &NBSP;&NBSP;&NBSP;&Nbsp;    return list;    } 

As you can see, code implementations that parse JSON data using Gson are also very simple. Among them, TypeToken is the data type converter provided by Gson, which supports a variety of data collection type conversions, and its reflection mechanism can be implemented to map the parsed Java objects to the corresponding data collection.

In this example, the same click button to send a request to the server to get the JSON data, after the server obtains the JSON data, using the above code to complete the parsing of the JSON data, and finally the resolved person object is displayed in the TextView control in turn. The program runs as shown in result 2.

650) this.width=650; "Src=" http://images.cnitblog.com/blog/430074/201305/ 19231730-51db738eed224a1d940a9e464dfd0c8b.jpg "width=" 356 "height=" 205 "style=" margin:0px Auto;padding:0px;border : 0px; "/>


Android Gson using--json2 objects and objects 2json

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.