What is JSON-lib?
Official explanation: JSON-lib is a Java library for transforming beans, maps, collections, Java arrays and XML to JSON and back again to beans and dynabeans.
In fact, the conversion between JSON format and Java class objects is realized. In Java code, you can easily parse data in JSON format.
Under what conditions do I use JSON-lib?
To parse strings in JSON format in Java code,
1. For example, strings in JSON format obtained through Ajax or from other interfaces;
2. I want to pass a complex string to the Java code for processing (for example, using an applet to read the content in the file ). Of course, in this way, you can write rules for parsing, frequently use split, and handle special characters. It is estimated that the head is big.
How do I start using JSON-lib?
Official Website: http://json-lib.sourceforge.net/
Jar package to be prepared
1.Json-lib-2.4-jdk15.jarThe latest version,
Http://sourceforge.net/projects/json-lib/files/
2. httpclient is a client programming toolkit that provides efficient, up-to-date, and rich functions to support HTTP. Http://hc.apache.org/downloads.cgi
3. commons Lang APIs provide basic and common operations and processing, such as automatically generating tostring () results. Http://commons.apache.org/lang/download_lang.cgi
4. commons logging Apache universal log tool http://commons.apache.org/logging/download_logging.cgi
5. commons collections provides a fairly complementary http://commons.apache.org/collections/download_collections.cgi for java standard collections APIs
6. commons beanutils uses the reflection mechanism to process attributes of JavaBean
Http://commons.apache.org/beanutils/download_beanutils.cgi
7. ezmorphIs a simple Java class library used to convert an object into another object http://sourceforge.net/projects/ezmorph/files/
Instance display
Java object ==> JSON
1. Java array => JSON string
String[] strArry = new String[]{"oscar1","oscar2","oscar3"};JSONArray jsArray = JSONArray.fromObject(strArry);String sJSONStr1 = jsArray.toString();System.out.println(sJSONStr1);
==> ["Oscar1", "oscar2", "oscar3"]
2. Java list => JSON string
List<String> list = new ArrayList<String>();list.add("id");list.add("name");JSONArray jsArray = JSONArray.fromObject(list);String sJSONStr = jsArray.toString();System.out.println(sJSONStr);
==> ["ID", "name"]
3. Java map ==> JSON string
Map<String,Object> map = new HashMap<String,Object>();map.put("id", 1);map.put("name", "Oscar");JSONObject json = JSONObject.fromObject(map);String sJSONStr1 = json.toString();System.out.println(sJSONStr1);
==>{ "ID": 1, "name": "Oscar "}
4. Use jsonobject and jsonarray ==> JSON string
JSONObject jsobj = new JSONObject();jsobj.put("id", 1);jsobj.put("name", "oscar");JSONArray jsArray = new JSONArray();JSONObject jsobj2 = new JSONObject();jsobj2.put("day1", "65");jsobj2.put("day2", "66");jsArray.add(jsobj2);jsobj.element("weight", jsArray);String sJSONStr = jsobj.toString();System.out.println(sJSONStr);
{"ID": 1, "name": "Oscar", "weight": [{"day1": "65", "day2": "66"}]}