Use of JSON, use of JSON
JSON has been widely used in projects recently. It is found that JSON and XML have similar functions and are both a data transmission format. JSON is more lightweight and easier to use than XML.
Third-party jar packages dependent on JSON:
Commons-beanutils.jar
Commons-collection.jar
Commons-lang.jar
Commons-logging.jar
Ezmorph-1.0.6.jar
Json-lib-2.4-jdk15.jar
Summary: The conversion between JSON strings and Java objects involves the following three situations:
(1) Conversion between Java Bean objects and JSON strings
(2) Conversion between Java arrays and JSON strings
(3) Conversion between Java List and JSON strings
The following is a JSON tool class I wrote:
1 package nuc. wss. util; 2 3 import java. util. list; 4 5 import net. sf. json. JSONArray; 6 import net. sf. json. JSONObject; 7 8/** 9*10 * Static Methods of the JsonUtil class encapsulate operations for conversion between Java objects and JSON strings 11 */12 public class JsonUtil {13/** 14 * JSON string converted to Java object 15*16 * @ param jsonString JSON string 17 * @ param clazz class18 * @ return corresponding to the Java object 19 */20 @ SuppressWarnings ("rawtypes ") 21 public static Object jsonStringToBean (String jsonString, Class clazz) {22 JSONObject jsonObject = JSONObject. fromObject (jsonString); 23 return JSONObject. toBean (jsonObject, clazz ); 24} 25 26/** 27 * convert the Bean object to a JSON string 28*29 * @ param object 30 * @ return returns the converted JSON string 31*/32 public static String beanToJsonString (Object object) {33 return JSONObject. fromObject (object ). toString (); 34} 35 36/** 37 * convert the JSON string to List38 * 39 * @ param jsonArrayString the converted JSON string 40 * @ param clazz List the type parameter corresponding to the class 41 * @ return returns the List object 42 */43 @ SuppressWarnings ("rawtypes ") 44 public static List jsonArrayStringToList (String jsonArrayString, Class clazz) {45 JSONArray jsonArray = JSONArray. fromObject (jsonArrayString); 46 return (List) JSONArray. toCollection (jsonArray, clazz ); 47} 48 49/** 50 * convert List object to JSON string 51*52 * @ param list convert List object 53 * @ return JSON string 54 */55 @ SuppressWarnings ("rawtypes ") 56 public static String listToJsonArrayString (List list) {57 return JSONArray. fromObject (list ). toString (); 58} 59 60/** 61 * convert a JSON string to a Java array 62*63 * @ param jsonArrayString the converted JSON string 64 * @ param clazz class 65 * @ return array 66 */67 @ SuppressWarnings ("rawtypes ") 68 public static Object [] jsonArrayStringToArray (String jsonArrayString, Class clazz) {69 JSONArray jsonArray = JSONArray. fromObject (jsonArrayString); 70 return (Object []) JSONArray. toArray (jsonArray, clazz ); 71} 72 73/** 74 * convert a Java array to a JSON String 75*76 * @ param array the converted array 77 * @ return JSON String 78 */79 public static String arrayToJsonArrayString (Object [] array) {80 return JSONArray. fromObject (array ). toString (); 81} 82}
Json usage
Json-format data can be obtained by clicking the "." operator directly to obtain the attributes and values. This only needs to be extracted using js Code. For example, json data is
Data = {name: "name", sex: "male"}; var name = data. name;
Then, submit the data using the request method, and then perform operations in java!
What are the benefits and usage of json?
1. It is easy to transmit with less redundant characters. Of course direct binary transfer is the best, but it is difficult to parse. It can also be xml or a pure string, but json has its unique advantages. Google has its own protocol called protobuf. If you are interested, please take a look.
2. Easy conversion. Many json APIs provide methods to convert json strings into objects and convert objects into json strings.
3. easy to read. A good json code structure allows you to intuitively understand what is stored.