Json-lib.jar Development Kit Uses:
Dependent Packages:
Commons-beanutils.jar;
Commons-httpclient.jar;
Commons-lang.jar;
Ezmorph.jar, a lot of people use will prompt net.sf.ezmorph.xxx can not find, is missing this:
Morph-1.0.1.jar
RELATED Links:
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
problems during use:
1, the old hint of the following error when converting beans to JSON format:
Exception in thread "main" Net.sf.json.JSONException:java.lang.NoSuchMethodException:Property ' name ' have no getter meth Od
FIX: Declare bean is public class XXX, must be public, I do not use the default type (class XXX)
2,exception in thread "main" Java.lang.NoSuchMethodError:org.apache.commons.lang.ArrayUtils.toObject ([C) [ljava/ Lang/character;
Reason: The definition attribute is as follows: Private char[] options = new char[] {' A ', ' f '}; It seems that this type of
3, private String func1 = "function (i) {return this.options[i];}";
And
Private Jsonfunction Func2 = new Jsonfunction (new string[] {"I"},
"Return this.options[i];");
The results are similar after conversion:
{"Func1": function (i) {return this.options[i];, "Func2": function (i) {return this.options[i];}}
Test class:
- import java.util.ArrayList;
- import Java.util.HashMap;
- import java.util.List;
- import Java.util.Map;
- import Net.sf.json.JSONArray;
- import Net.sf.json.JSONObject;
- Public class Json {
- Public Static void Main (string[] args) {
- JSON j = New json ();
- J.bean2json ();
- }
- Public void Arr2json () {
- Boolean [] Boolarray = new boolean[] { true, false, true };
- Jsonarray Jsonarray = Jsonarray.fromobject (Boolarray);
- System.out.println (Jsonarray);
- Prints [true,false,true]
- }
- Public void List2json () {
- List List = new ArrayList ();
- List.add ("first");
- List.add ("second");
- Jsonarray Jsonarray = jsonarray.fromobject (list);
- System.out.println (Jsonarray);
- Prints ["First", "second"]
- }
- Public void Createjson () {
- Jsonarray Jsonarray = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");
- System.out.println (Jsonarray);
- Prints ["JSON", "is", "easy"]
- }
- Public void Map2json () {
- Map
- Map.put ("name", "JSON");
- Map.put ("bool", boolean.true);
- Map.put ("int", new Integer (1));
- Map.put ("arr", new string[] {"A", "B"});
- Map.put ("func", "function (i) {return this.arr[i];}");
- Jsonobject JSON = jsonobject.fromobject (map);
- SYSTEM.OUT.PRINTLN (JSON);
- Prints
- ["Name": "JSON", "bool": TRUE, "int": 1, "arr": ["a", "B"], "func": function (i) {
- return this.arr[i]; }]
- }
- Public void Bean2json () {
- Jsonobject jsonobject = Jsonobject.fromobject (new Mybean ());
- System.out.println (Jsonobject);
- /*
- * Prints
- * {"func1": function (i) {return this.options[i];
- *}, "pojoid": 1, "name": "JSON", "FUNC2": function (i) {return
- * This.options[i]; }}
- */
- }
- Public void Json2bean () {
- String json = "{name=/" json2/", Func1:true,pojoid:1,func2:function (a) {return A;},options:[' 1 ', ' 2 ']}";
- Jsonobject JB = jsonobject.fromstring (JSON);
- Jsonobject.tobean (JB, Mybean. Class);
- System.out.println ();
- }
- }
Bean to manipulate:
- import net.sf.json.JSONFunction;
- Public class Mybean {
- Private String name = "JSON";
- Private int pojoid = 1;
- Private char[] options = new char[] {' A ', ' F '};
- Private String func1 = "function (i) {return this.options[i];}";
- Private Jsonfunction Func2 = new jsonfunction (new string[] {"I"},
- "Return this.options[i];");
- Getters & Setters
- ......
- }
Use Methods of JSON
JSON, or JavaScript Object natation, is a lightweight data interchange format that is ideal for server-to-javascript interaction. This article will quickly explain the JSON format and demonstrate how to handle JSON-formatted data on both the client and server side, using code examples.
JSON-Required Packages
Commons-httpclient-3.1.jar
Commons-lang-2.4.jar
Commons-logging-1.1.1.jar
Json-lib-2.2.3-jdk13.jar
Ezmorph-1.0.6.jar
Commons-collections-3.2.1.jar
The above package can be
Http://commons.apache.org/index.html
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
http://www.docjar.com/
Downloaded to the.
The Java.lang.noclassdeffounderror:net/sf/ezmorph/morpher error occurred because the Ezmorph.jar file was not imported or the version is incorrect.
Appears java.lang.noclassdeffounderror:org/apache/commons/collections/map/ The LISTORDEREDMAP error is because the Commons-collections.jar file is not imported or the version is incorrect.
Translating Java code into JSON code
1. The list collection is converted into JSON code
List List = new ArrayList (); List.add ("first"); List.add ("second"); Jsonarray jsonArray2 = Jsonarray. Fromobject (list); |
2. Map collection into JSON code
Map map = new HashMap (); Map.put ("name", "JSON"); Map.put ("bool", Boolean. TRUE); Map.put ("int", new Integer (1)); Map.put ("arr", new string[] {"A", "B"}); Map.put ("func", "function (i) {return this.arr[i];}"); Jsonobject json = Jsonobject. Fromobject (map); |
3. Bean converted to JSON code
Jsonobject jsonobject = jsonobject.fromobject (New Jsonbean ()); |
4. Convert arrays into JSON code
Boolean [] Boolarray = new boolean[] { true, false, true }; Jsonarray jsonArray1 = Jsonarray. Fromobject (Boolarray); |
5. Conversion of general data into JSON code
Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']"); |
6. Beans conversion to JSON code
List List = new ArrayList (); JsonBean2 jb1 = new JsonBean2 (); Jb1.setcol (1); Jb1.setrow (1); Jb1.setvalue ("xx"); JsonBean2 jb2 = new JsonBean2 (); Jb2.setcol (2); Jb2.setrow (2); Jb2.setvalue (""); List.add (JB1); List.add (JB2); Jsonarray ja = jsonarray. Fromobject (list); |
Json-lib Bag Notes