http://blog.csdn.net/huangwuyi/article/details/5412500
1.JAR Package Introduction
To enable the program to run, the Json-lib package must be introduced, and the Json-lib package relies on the following jar packages:
- Commons-lang.jar
- Commons-beanutils.jar
- Commons-collections.jar
- Commons-logging.jar
- Ezmorph.jar
- Json-lib-2.2.2-jdk15.jar
2.JSONObject Object Use
The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON transforms each other's packages. In this example, we will use the Jsonobject class to create the Jsonobject object, and then we print the values of those objects. In order to use the Jsonobject object, we will introduce the "Net.sf.json" package. To add elements to an object, we use the put () method.
2.1. Example 1
Package jsontest;
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
public class Jsonobjectsample {
Create a Jsonobject object
private static Jsonobject Createjsonobject () {
Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put ("username", "Huangwuyi");
Jsonobject.put ("Sex", "male");
Jsonobject.put ("QQ", "413425430");
Jsonobject.put ("Min.score", New Integer (99));
Jsonobject.put ("nickname", "Mood in the Dream");
return jsonobject;
}
public static void Main (string[] args) {
Jsonobject jsonobject = Jsonobjectsample.createjsonobject ();//static method, directly through the class name + method call
Output Jsonobject Object
System.out.println ("Jsonobject:" + jsonobject);
Interpreting the type of the output object
Boolean IsArray = Jsonobject.isarray ();
Boolean isEmpty = Jsonobject.isempty ();
Boolean isnullobject = Jsonobject.isnullobject ();
System.out.println ("is an array:" + IsArray + ", is empty:" + isEmpty
+ ", Isnullobject:" + isnullobject);
Adds an attribute, appending an element after jsonobject.
Jsonobject.element ("Address", "Xiamen, Fujian Province");
System.out.println ("Object after adding attribute:" + jsonobject);
Returns a Jsonarray object
Jsonarray Jsonarray = new Jsonarray ();
Jsonarray.add (0, "This is a Jsonarray value");
Jsonarray.add (1, "another Jsonarray value");
Jsonobject.element ("Jsonarray", Jsonarray);
In the back of Jsonobject a Jsonarray
Jsonarray array = Jsonobject.getjsonarray ("Jsonarray");
System.out.println (Jsonobject);
System.out.println ("Returns a Jsonarray object:" + array);
Value after adding Jsonarray
{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "The Heart of the Dream", "Address": "Xiamen, Fujian province", " Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]}
SYSTEM.OUT.PRINTLN ("result =" + Jsonobject);
Returns a string based on key
String username = jsonobject.getstring ("username");
System.out.println ("username==>" + username);
Convert characters to Jsonobject
String temp = jsonobject.tostring ();
Jsonobject object = jsonobject.fromobject (temp);
Return value after conversion based on key
System.out.println ("qq=" + object.get ("QQ"));
}
}
Output results
jsonobject:{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "A", "nickname": "The Mood in the Dream"}
is an array: false, whether it is empty: false, Isnullobject:false
Objects after adding attributes: {"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "The Heart of the Dream", "Address": "Xiamen, Fujian Province" "}
{"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "The Heart of the Dream", "Address": "Xiamen, Fujian province", " Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]}
Returns a Jsonarray object: ["This is a Jsonarray value", "another Jsonarray value"]
Results ={"username": "Huangwuyi", "Sex": "Male", "QQ": "413425430", "Min.score": "The Heart of the Dream", "Address": "Xiamen, Fujian province", " Jsonarray ": [" This is a Jsonarray value "," another Jsonarray value "]}
Username==>huangwuyi
qq=413425430
2.2. Example 2.
Package jsontest;
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
public class Jsontest {
public static void Main (String args[])
{
Jsonobject jsonObj0 = new Jsonobject ();
Jsonobject jsonobj = new Jsonobject ();
Jsonobject jsonObj2 = new Jsonobject ();
Jsonobject jsonObj3 = new Jsonobject ();
Jsonarray Jsonarray = new Jsonarray ();
Create JsonObj0
Jsonobj0.put ("NAME0", "Zhangsan");
Jsonobj0.put ("Sex1", "female");
System.out.println ("jsonObj0:" +jsonobj0);
Create JsonObj1
Jsonobj.put ("name", "Xuwei");
Jsonobj.put ("Sex", "male");
System.out.println ("Jsonobj:" +jsonobj);
Create a jsonObj2 with two entries with entries of Jsonobj0,jsonobj1
Jsonobj2.put ("Item0", jsonObj0);
Jsonobj2.put ("Item1", jsonobj);
System.out.println ("JsonObj2:" +jsonobj2);
Create jsonObj3 with only one entry with content JsonObj2
Jsonobj3.element ("J3", jsonObj2);
System.out.println ("jsonObj3:" +jsonobj3);
Add the Jsonobject object to the Jsonarray. The difference between Jsonarray and Jsonobject is that Jsonarray is more than jsonobject square brackets []
Jsonarray.add (Jsonobj);
System.out.println ("Jsonarray:" +jsonarray);
Jsonobject jsonObj4 = new Jsonobject ();
Jsonobj4.element ("Weather", jsonarray);
System.out.println ("JsonObj4:" +jsonobj4);
}
}
Output Result:
jsonobj0:{"NAME0": "Zhangsan", "sex1": "Female"}
jsonobj:{"name": "Xuwei", "Sex": "Male"}
jsonobj2:{"Item0": {"NAME0": "Zhangsan", "sex1": "Female"}, "Item1": {"name": "Xuwei", "Sex": "Male"}}
jsonobj3:{"J3": {"item0": {"NAME0": "Zhangsan", "sex1": "Female"}, "Item1": {"name": "Xuwei", "Sex": "Male"}}
jsonarray:[{"name": "Xuwei", "Sex": "Male"}]
jsonobj4:{"Weather": [{"Name": "Xuwei", "Sex": "Male"}]}
The use of Jsonobject and Jsonarray