Use of JSON

Source: Internet
Author: User

JSON has recently been used extensively in the project, and it is found that JSON and XML are similar in functionality and are a data transfer format. JSON is more lightweight and easier to use than XML.

JSON-dependent third-party jar packages:

Commons-beanutils.jar
Commons-collection.jar
Commons-lang.jar
Commons-logging.jar
Ezmorph-1.0.6.jar
Json-lib-2.4-jdk15.jar

It concludes that there are three scenarios in which JSON strings are converted to Java objects in the following ways:

(1) Conversion between Java Bean object and JSON string

(2) conversion between Java arrays and JSON strings

(3) The conversion between Java list and JSON string

Here is a JSON tool Class I wrote:

1  PackageNuc.wss.util;2 3 Importjava.util.List;4 5 ImportNet.sf.json.JSONArray;6 ImportNet.sf.json.JSONObject;7 8 /**9  * Ten * The static method of the Jsonutil class encapsulates the operation of translating Java objects with JSON strings One  */ A  Public classJsonutil { -     /** - * Convert a JSON string into a Java object the      *  -      * @paramjsonstring JSON string -      * @paramclazz Java object corresponding to class -      * @returnreturning Java Objects +      */ -@SuppressWarnings ("Rawtypes") +      Public StaticObject Jsonstringtobean (String jsonstring, Class clazz) { AJsonobject Jsonobject =Jsonobject.fromobject (jsonstring); at         returnJsonobject.tobean (Jsonobject, clazz);  -     } -      -     /** - * Convert Bean object to JSON string -      *  in      * @paramobject that will be converted -      * @returnreturns the converted JSON string to      */ +      Public StaticString beantojsonstring (Object object) { -         returnJsonobject.fromobject (object). toString (); the     } *      $     /**Panax Notoginseng * Convert JSON string to list -      *  the      * @paramjsonarraystring the converted JSON string +      * @paramclass for the type parameter of the Clazz list A      * @returnreturns the list object the      */ +@SuppressWarnings ("Rawtypes") -      Public StaticList jsonarraystringtolist (String jsonarraystring, Class clazz) { $Jsonarray Jsonarray =Jsonarray.fromobject (jsonarraystring); $         return(List) jsonarray.tocollection (Jsonarray, clazz); -     } -      the     /** - * Convert List object to JSON stringWuyi      *  the      * @paramList object that the list will be converted to -      * @returnreturns a JSON string Wu      */ -@SuppressWarnings ("Rawtypes") About      Public StaticString listtojsonarraystring (List list) { $         returnjsonarray.fromobject (list). toString (); -     } -      -     /** A * Converts a JSON string into a Java array +      *  the      * @paramjsonarraystring The JSON string that was converted -      * @paramclazz classes corresponding to array types $      * @returnArray the      */ the@SuppressWarnings ("Rawtypes") the      Public Staticobject[] Jsonarraystringtoarray (String jsonarraystring, Class clazz) { theJsonarray Jsonarray =Jsonarray.fromobject (jsonarraystring); -         return(object[]) Jsonarray.toarray (Jsonarray, clazz); in     } the      the     /** About * Convert Java arrays to JSON strings the      *  the      * @paramarray of converted arrays the      * @returnJSON string +      */ -      Public StaticString arraytojsonarraystring (object[] array) { the         returnjsonarray.fromobject (array). toString ();Bayi     } the}

Use of JSON

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.