Based on json-lib.jar package JSON program practice

Source: Internet
Author: User
Tags package json

Based on the json-lib.jar package JSON program, this article mainly introduces a simple example!
1. First, JSON-lib must support at least the following JAR packages
Jakarta commons-lang 2.4
Jakarta commons-beanutils 1.7.0
Jakarta commons-collections 3.2
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6

2. arrays of the Java Collection type. collections is converted to jsonarray.
Example 1:
Boolean [] boolarray = new Boolean [] {true, false, true };
Jsonarray = jsonarray. fromobject (boolarray );
System. Out. println (jsonarray );
Output: [true, false, true]

Example 2:
List list = new arraylist ();
List. Add ("first ");
List. Add ("second ");
Jsonarray = jsonarray. fromobject (list );
System. Out. println (jsonarray );
Output: ["first", "second"]

Example 3:
Jsonarray = jsonarray. fromobject ("['json', 'is', 'easy']");
System. Out. println (jsonarray );
Output: ["JSON", "is", "easy"]

3. Conversion of Java object type JavaBean and maps with jsonobject
Example 1:
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 = jsonobject. fromobject (MAP );
System. Out. println (jsonobject );
Output: {"func": function (I) {return this. arr [I] ;}, "arr": ["A", "B"], "int": 1, "bool": True, "name ": "JSON "}

Example 2:
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
...
}
Jsonobject = jsonobject. fromobject (New mybean ());
System. Out. println (jsonobject );
Output: {"func1": function (I) {return this. options [I];}, "func2": function (I) {return this. options [I];}, "name": "JSON", "options": ["A", "F"], "pojoid": 1}

4. Convert JSON Data Object format to Java type beans
Example 1 (convert to dynamic bean ):
String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";
Jsonobject = jsonobject. fromobject (JSON );
Object bean = jsonobject. tobean (jsonobject );
Assertequals (jsonobject. Get ("name"), propertyutils. getproperty (bean, "name "));
Assertequals (jsonobject. Get ("bool"), propertyutils. getproperty (bean, "bool "));
Assertequals (jsonobject. Get ("int"), propertyutils. getproperty (bean, "int "));
Assertequals (jsonobject. Get ("double"), propertyutils. getproperty (bean, "double "));
Assertequals (jsonobject. Get ("func"), propertyutils. getproperty (bean, "func "));
Output: The JUnit test shows green bars, that is, the values are equal.

Example 2 (convert to a specific bean ):
String JSON = "{bool: True, integer: 1, string: \" JSON \"}";
Jsonobject = jsonobject. fromobject (JSON );
Beana bean = (beana) jsonobject. tobean (jsonobject, beana. Class );
Assertequals (jsonobject. Get ("bool"), Boolean. valueof (bean. isbool ()));
Assertequals (jsonobject. Get ("integer"), new INTEGER (bean. getinteger ()));
Assertequals (jsonobject. Get ("string"), Bean. getstring ());
Note: beana is the getters & setters Method for specific attributes.

5. Java-JSON mutual conversion filter-filter attributes when converting Java type to JSON. Here we will demonstrate instance 1 in 3:
Instance 1:
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];}");

Jsonconfig = new jsonconfig ();
Jsonconfig. setjsonpropertyfilter (New propertyfilter (){
Public Boolean apply (Object source, string name, object Value ){
If (value! = NULL & number. Class. isassignablefrom (value. getclass ())){
Return true;
}
Return false;
}
});
Jsonobject JSON = jsonobject. fromobject (MAP, jsonconfig );
System. Out. println (JSON );
Output: {"func": function (I) {return this. arr [I] ;}, "arr": ["A", "B"], "bool": True, "name": "JSON "}
And the above output: {"func": function (I) {return this. arr [I] ;}, "arr": ["A", "B"], "int": 1, "bool": True, "name ": "JSON "}
It is missing: "int": 1, this section, this is the role of the attribute filter, you can see the code to know it to filter out the value of the number type.

Instance 2:
What if we change number. Class. isassignablefrom (value. getclass () in the above example to string?
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];}");

Jsonconfig = new jsonconfig ();
Jsonconfig. setjsonpropertyfilter (New propertyfilter (){
Public Boolean apply (Object source, string name, object Value ){
If (value! = NULL & string. Class. isassignablefrom (value. getclass () {// The Key To Filtering
Return true;
}
Return false;
}
});
Jsonobject JSON = jsonobject. fromobject (MAP, jsonconfig );
System. Out. println (JSON );
Output: {"arr": ["A", "B"], "int": 1, "bool ": true} // It filters out all values of the string type.

6. JSON-Java mutual conversion filter-same as 5 this time -- filter attributes when converting JSON-to Java. Here we will demonstrate instance 1 in 4:
Instance 1:
String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";
Jsonobject = jsonobject. fromobject (JSON );

Jsonconfig = new jsonconfig ();
Jsonconfig. setrootclass (Map. Class );
Jsonconfig. setjavapropertyfilter (New propertyfilter (){
Public Boolean apply (Object source, string name, object Value ){
If ("bool". Equals (name) | "double". Equals (name) {// The Key To Filtering
Return true;
}
Return false;
}
});
Object bean = jsonobject. tobean (jsonobject, jsonconfig );
System. Out. println (bean );
Output: {func = function (a) {return a ;}, Int = 1, name = JSON, array = [1, 2]}
Same as instance 1 Output in 4: {double = 2.2, func = function (a) {return a ;}, Int = 1, name = JSON, bool = true, array = [1, 2]}
Missing: Double = 2.2, bool = true, because the items with name bool and double have been filtered out.

Instance 2:
String JSON = "{name = \" JSON \ ", bool: True, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: [1, 2]} ";
Jsonobject = jsonobject. fromobject (JSON );
Jsonconfig = new jsonconfig ();
Jsonconfig. setrootclass (Map. Class );
Jsonconfig. setjavapropertyfilter (New propertyfilter (){
Public Boolean apply (Object source, string name, object Value ){
If (value! = NULL & string. Class. isassignablefrom (value. getclass () {// The Key To Filtering
Return true;
}
Return false;
}
});
Object bean = jsonobject. tobean (jsonobject, jsonconfig );
System. Out. println (bean );
Output: {double = 2.2, func = function (a) {return a ;}, Int = 1, bool = true, array = [1, 2]}

It can be seen that, whether Java is converted to JSON or JSON to Java, the filter can be filtered by name and value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.