1. Introduction to jar packages
To run a program, the JSON-lib package must be introduced. The JSON-lib package depends on the following jar package:
1. commons-lang.jar
2. commons-beanutils.jar
3. commons-collections.jar
4. commons-logging.jar
5. ezmorph. Jar
6. json-lib-2.2.2-jdk15.jar
II,
Use jsonobject
JSON-
The LIB package is a bean, collections, maps, Java Arrays
Packages that are converted to 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
Jsonobject. We will introduce the "net. SF. JSON" package. To add elements to an object, we need to use the put () method.
Java code
- 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 ("name"
,
"Kevin"
);
- Jsonobject. Put ("Max. Score"
,
New
INTEGER (
100
));
- Jsonobject. Put ("Min. Score"
,
New
INTEGER (
50
));
- Jsonobject. Put ("nickname"
,
"Picglet"
);
- Return
Jsonobject;
- }
- Public
Static
Void
Main (string [] ARGs ){
- Jsonobject = jsonobjectsample. createjsonobject ();
- // 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 ("isarray :"
+ Isarray +
"Isempty :"
+ Isempty +
"Isnullobject :"
+ Isnullobject );
- // Add attributes
- Jsonobject. element ("Address"
,
"Swap Lake"
);
- 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 );
- Jsonarray array = jsonobject. getjsonarray ("jsonarray"
);
- System. Out. println ("A jsonarray object is returned :"
+ Array );
- // Value after jsonarray is added
- // {"Name": "Kevin", "Max. score ": 100," Min. score ": 50," nickname ":" picglet "," Address ":" swap Lake ",
- // "Jsonarray": ["This Is A jsonarray value", "another jsonarray value"]}
- System. Out. println (jsonobject );
- // Returns a string based on the key.
- String jsonstring = jsonobject. getstring ("name"
);
- System. Out. println ("jsonstring =>"
+ Jsonstring );
- }
- }
Import net. SF. JSON. jsonarray; import net. SF. JSON. jsonobject; public class jsonobjectsample {// create jsonobject object Private Static jsonobject createjsonobject () {jsonobject = new jsonobject (); jsonobject. put ("name", "Kevin"); jsonobject. put ("Max. score ", new INTEGER (100); jsonobject. put ("Min. score ", new INTEGER (50); jsonobject. put ("nickname", "picglet"); Return jsonobject;} public static void main (string [] ARGs) {jsonobject = jsonobjectsample. createjsonobject (); // output the jsonobject system. out. println ("jsonobject =>" + jsonobject); // specifies the type of the output object. boolean isarray = jsonobject. isarray (); Boolean isempty = jsonobject. isempty (); Boolean isnullobject = jsonobject. isnullobject (); system. out. println ("isarray:" + isarray + "isempty:" + isempty + "isnullobject:" + isnullobject); // Add the jsonobject attribute. element ("Address", "swap Lake"); system. out. println ("added object =>" + 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); jsonarray array = jsonobject. getjsonarray ("jsonarray"); system. out. println ("return a jsonarray object:" + array); // value after jsonarray is added // {"name": "Kevin", "Max. score ": 100," Min. score ": 50," nickname ":" picglet "," Address ":" swap Lake ", //" jsonarray ": [" This Is A jsonarray value ", "Another jsonarray value"]} system. out. println (jsonobject); // returns a string jsonstring = jsonobject based on the key. getstring ("name"); system. out. println ("jsonstring =>" + jsonstring );}}
After getting the jsonobject object, we can use its method. You can view its API. I will provide an online API
Http://json-lib.sourceforge.net/apidocs/jdk15/index.html