Document directory
- 2. 1. instance 1
- 2. instance 2.
- 2. 1. instance 1
- 2. instance 2.
Use of jsonobject and jsonarray 1. jar package Introduction
To run a program, the JSON-lib package must be introduced. The JSON-lib package depends on the following jar package:
- Commons-lang.jar
- Commons-beanutils.jar
- Commons-collections.jar
- Commons-logging.jar
- Ezmorph. Jar
- Json-lib-2.2.2-jdk15.jar
2. Use jsonobject
The JSON-lib package is a package that converts beans, collections, maps, Java arrays, XML, and JSON. In this example, we will use the jsonobject class to create jsonobject objects, and then print the values of these objects. To use a jsonobject, we need to introduce the "net. SF. JSON" package. To add elements to an object, we need to use the put () method.
2. 1. instance 1
Package jsontest;
Import net. SF. JSON. jsonarray;
Import net. SF. JSON. jsonobject;
Public class jsonobjectsample {
// Create a jsonobject
Private Static jsonobject createjsonobject (){
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", "Dream mood ");
Return jsonobject;
}
Public static void main (string [] ARGs ){
Jsonobject = jsonobjectsample. createjsonobject (); // wait for the method to be called directly by class name + Method
// Output the jsonobject object
System. Out. println ("jsonobject:" + jsonobject );
// Interpret the type of the output object
Boolean isarray = jsonobject. isarray ();
Boolean isempty = jsonobject. isempty ();
Boolean isnullobject = jsonobject. isnullobject ();
System. Out. println ("whether it is an array:" + isarray + ", whether it is empty:" + isempty
+ ", Isnullobject:" + isnullobject );
// Add attributes and add elements to the end of jsonobject.
Jsonobject. element ("Address", "Xiamen City, Fujian Province ");
System. Out. println ("object after adding attribute:" + jsonobject );
// Return a jsonarray object
Jsonarray = new jsonarray ();
Jsonarray. Add (0, "This is a jsonarray value ");
Jsonarray. Add (1, "another jsonarray value ");
Jsonobject. element ("jsonarray", jsonarray );
// Build a jsonarray behind the jsonobject
Jsonarray array = jsonobject. getjsonarray ("jsonarray ");
System. Out. println (jsonobject );
System. Out. println ("return a jsonarray object:" + array );
// Value after jsonarray is added
// {"Username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
System. Out. println ("result =" + jsonobject );
// Returns a string based on the key.
String username = jsonobject. getstring ("username ");
System. Out. println ("username =>" + username );
// Convert the character to jsonobject
String temp = jsonobject. tostring ();
Jsonobject object = jsonobject. fromobject (temp );
// Return value based on key after conversion
System. Out. println ("QQ =" + object. Get ("QQ "));
}
}
Output result
Jsonobject: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "}
Array: false, empty: false, isnullobject: false
Object after adding attribute: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "}
{"Username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
Returns a jsonarray object: ["This Is A jsonarray value", "another jsonarray value"]
Result = {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
Username ==> huangwuyi
Qq = 1, 413425430
2. instance 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 = 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 jsonobj2, which contains two entries: jsonobj0 and jsonobj1.
Jsonobj2.put ("item0", jsonobj0 );
Jsonobj2.put ("Item1", jsonobj );
System. Out. println ("jsonobj2:" + jsonobj2 );
// Create jsonobj3 with only one entry. The content is jsonobj2.
Jsonobj3.element ("J3", jsonobj2 );
System. Out. println ("jsonobj3:" + jsonobj3 );
// Add a jsonobject to jsonarray. The difference between jsonarray and jsonobject is that jsonarray has more brackets than jsonobject []
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"}]}
To run a program, the JSON-lib package must be introduced. The JSON-lib package depends on the following jar package:
- Commons-lang.jar
- Commons-beanutils.jar
- Commons-collections.jar
- Commons-logging.jar
- Ezmorph. Jar
- Json-lib-2.2.2-jdk15.jar
2. Use jsonobject
The JSON-lib package is a package that converts beans, collections, maps, Java arrays, XML, and JSON. In this example, we will use the jsonobject class to create jsonobject objects, and then print the values of these objects. To use a jsonobject, we need to introduce the "net. SF. JSON" package. To add elements to an object, we need to use the put () method.
2. 1. instance 1
Package jsontest;
Import net. SF. JSON. jsonarray;
Import net. SF. JSON. jsonobject;
Public class jsonobjectsample {
// Create a jsonobject
Private Static jsonobject createjsonobject (){
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", "Dream mood ");
Return jsonobject;
}
Public static void main (string [] ARGs ){
Jsonobject = jsonobjectsample. createjsonobject (); // wait for the method to be called directly by class name + Method
// Output the jsonobject object
System. Out. println ("jsonobject:" + jsonobject );
// Interpret the type of the output object
Boolean isarray = jsonobject. isarray ();
Boolean isempty = jsonobject. isempty ();
Boolean isnullobject = jsonobject. isnullobject ();
System. Out. println ("whether it is an array:" + isarray + ", whether it is empty:" + isempty
+ ", Isnullobject:" + isnullobject );
// Add attributes and add elements to the end of jsonobject.
Jsonobject. element ("Address", "Xiamen City, Fujian Province ");
System. Out. println ("object after adding attribute:" + jsonobject );
// Return a jsonarray object
Jsonarray = new jsonarray ();
Jsonarray. Add (0, "This is a jsonarray value ");
Jsonarray. Add (1, "another jsonarray value ");
Jsonobject. element ("jsonarray", jsonarray );
// Build a jsonarray behind the jsonobject
Jsonarray array = jsonobject. getjsonarray ("jsonarray ");
System. Out. println (jsonobject );
System. Out. println ("return a jsonarray object:" + array );
// Value after jsonarray is added
// {"Username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
System. Out. println ("result =" + jsonobject );
// Returns a string based on the key.
String username = jsonobject. getstring ("username ");
System. Out. println ("username =>" + username );
// Convert the character to jsonobject
String temp = jsonobject. tostring ();
Jsonobject object = jsonobject. fromobject (temp );
// Return value based on key after conversion
System. Out. println ("QQ =" + object. Get ("QQ "));
}
}
Output result
Jsonobject: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "}
Array: false, empty: false, isnullobject: false
Object after adding attribute: {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "}
{"Username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
Returns a jsonarray object: ["This Is A jsonarray value", "another jsonarray value"]
Result = {"username": "huangwuyi", "sex": "male", "QQ": "413425430", "Min. score ": 99," nickname ":" Dream mood "," Address ":" Xiamen City, Fujian Province "," jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]}
Username ==> huangwuyi
Qq = 1, 413425430
2. instance 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 = 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 jsonobj2, which contains two entries: jsonobj0 and jsonobj1.
Jsonobj2.put ("item0", jsonobj0 );
Jsonobj2.put ("Item1", jsonobj );
System. Out. println ("jsonobj2:" + jsonobj2 );
// Create jsonobj3 with only one entry. The content is jsonobj2.
Jsonobj3.element ("J3", jsonobj2 );
System. Out. println ("jsonobj3:" + jsonobj3 );
// Add a jsonobject to jsonarray. The difference between jsonarray and jsonobject is that jsonarray has more brackets than jsonobject []
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"}]}