Json-lib use

Source: Internet
Author: User

    • Json-lib

Json-lib is a Java class library (official website: http://json-lib.sourceforge.net/) to achieve the following functions:

    • Convert JavaBeans, maps, collections, Java arrays, and XML into JSON format data
    • Convert JSON format data to a JavaBeans object

Json-lib Required JAR Package

    • Commons-beanutils-1.8.3.jar
    • Commons-collections-3.2.1.jar
    • Commons-lang-2.6.jar
    • Commons-logging-1.1.1.jar
    • Ezmorph-1.0.6.jar
    • Json-lib-2.4-jdk15.jar

    • Use of Json-lib

1. Parse the Array into a Json string.

@Test
public void Arrtojson () {
/**
* Convert array to JSON string
*/
string[] str = {"Json", "Json-lib", "Jackson", "Xml"};
Jsonarray JSON = Jsonarray.fromobject (str);
SYSTEM.OUT.PRINTLN (JSON);

/**
* Object array to JSON
*/
Person[] ps = {new person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}),
New Person (2, "haha", false, New Date (), Arrays.aslist (new string[] {"Play"}))
};
JSON = Jsonarray.fromobject (PS);
SYSTEM.OUT.PRINTLN (JSON);

/**
* Convert List collection to JSON
*/
list<person> plist = new arraylist<person> ();
Plist.add (New person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}));
Plist.add (New person (2, "haha", false, New Date (), Arrays.aslist (new string[] {"Play"}));
JSON = Jsonarray.fromobject (PS);
SYSTEM.OUT.PRINTLN (JSON);

}

The results of the operation are as follows:

["Json", "Json-lib", "Jackson", "Xml"]
[{"Birthday": {"date": +, "Day": 5, "hours": +, "minutes": +, "month": 3, "Seconds": "," "Time": 1492169396320, " Timezoneoffset ": -480," Year ": 117}," Gender ": false," hobby ": [" Play "," Game "," Anime "]," id ": 1," name ":" Slightly "},{" birthday ": {" Date ":" Day ": 5," hours ": +," minutes ":" Month ": 3," Seconds ":" "," "Time: 1492169396320," Timezoneoffset ":-480," Year ": 117}," Gender ": false," hobby ": [" Play "]," id ": 2," name ":" Haha "}]
[{"Birthday": {"date": +, "Day": 5, "hours": +, "minutes": +, "month": 3, "Seconds": "," "Time": 1492169396320, " Timezoneoffset ": -480," Year ": 117}," Gender ": false," hobby ": [" Play "," Game "," Anime "]," id ": 1," name ":" Slightly "},{" birthday ": {" Date ":" Day ": 5," hours ": +," minutes ":" Month ": 3," Seconds ":" "," "Time: 1492169396320," Timezoneoffset ":-480," Year ": 117}," Gender ": false," hobby ": [" Play "]," id ": 2," name ":" Haha "}]

2. Parse the Javabean/map into a JSON string. Using Jsonobject parsing:

@Test
public void Beantojson () {

/**
* Convert objects to JSON
*/
Person p = new person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}));
Jsonobject json = Jsonobject.fromobject (p);
SYSTEM.OUT.PRINTLN (JSON);

/**
* Parse Map
*/
map<string, object> map = new hashmap<string, object> ();
Map.put ("name", "Director III");
Map.put ("Age", 24);
JSON = Jsonobject.fromobject (map);
SYSTEM.OUT.PRINTLN (JSON);

}

The results of the operation are as follows:

{"Birthday": {"date": +, "Day": 5, "hours": +, "minutes": +, "month": 3, "Seconds": "Time": 1492169902753, " Timezoneoffset ": -480," Year ": 117}," Gender ": false," hobby ": [" Play "," Game "," Anime "]," id ": 1," name ":" Slightly "}
{"Age": +, "name": "Director Three"}

3. Using the Jsonconfig-over property: for Javabean/map

@Test
public void Jsonconfig () {
/**
* Filter Properties using Jsonconfig
*/
Person p = new person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}));
Jsonconfig config = new Jsonconfig ();
Config.setexcludes (new string[] {"Birthday"});
Jsonobject json = jsonobject.fromobject (P, config);
SYSTEM.OUT.PRINTLN (JSON);
}

The result of the operation is as follows, and we can see that the name attribute is filtered out in the running result:

{"Gender": false, "hobby": ["Play", "Game", "Anime"], "id": 1, "name": "Slightly"}

4. Convert the Json string to an Array:

@Test
public void Jsontoarr () {

/**
* JSON converted to a generic array
*/
string[] str = {"Json", "Json-lib", "Jackson", "Xml"};
Jsonarray JSON = Jsonarray.fromobject (str);

Object STRs = Jsonarray.toarray (JSON);
System.out.println (STRs);
System.out.println (Arrays.aslist ((object[)) STRs);

list<person> plist = new arraylist<person> ();
Plist.add (New person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}));
Plist.add (New person (2, "haha", false, New Date (), Arrays.aslist (new string[] {"Play"}));
JSON = Jsonarray.fromobject (plist);

list<person> PS = (list<person>) jsonarray.tocollection (JSON, person.class);
for (person Person:ps) {
SYSTEM.OUT.PRINTLN (person);
}
}

The results of the operation are as follows:

[ljava.lang.object;@39e87719
[Json, Json-lib, Jackson, Xml]
person [id=1, Name=, Gender=false, Birthday=fri Apr 20:18:29 CST, hobby=[play, game, anime]]
person [id=2, name= haha, gender=false, Birthday=fri Apr 20:18:29 CST, hobby=[Play]]

5. Turn the Json string into JavaBean:

@Test
public void Jsontobean () {
/**
* JSON converted to JavaBean
*/
Person p = new person (1, "slight", false, New Date (), Arrays.aslist (new string[] {"Play", "Game", "Anime"}));
Jsonobject json = Jsonobject.fromobject (p);

Object o = Jsonobject.tobean (JSON, person.class);
System.out.println (o);



}

The results of the operation are as follows:

2017-4-14 20:19:51 Net.sf.json.JSONObject Tobean
Info: Property ' Day ' of class java.util.Date has no write method. Skipped.
2017-4-14 20:19:51 Net.sf.json.JSONObject Tobean
Information: Property ' Timezoneoffset ' of class java.util.Date have no write method. Skipped.
person [id=1, Name=, Gender=false, Birthday=fri Apr 20:19:51 CST, hobby=[play, game, anime]]

When converting a Json-like string to JavaBean, it is important to note that there must be no parameter constructor in the JavaBean, or the following error cannot be found for the initialization method:

Exception in thread "main" Net.sf.json.jsonexception:java.lang.nosuchmethodexception:cn.sunzn.json.person.<init > () at   Net.sf.json.JSONObject.toBean (jsonobject.java:288) at   Net.sf.json.JSONObject.toBean ( jsonobject.java:233) at   Cn.sunzn.json.JsonLib.main (jsonlib.java:23) caused by:java.lang.NoSuchMethodException : Cn.sunzn.json.person.<init> () at   java.lang.Class.getConstructor0 (Unknown Source)   at Java.lang.Class.getDeclaredConstructor (Unknown Source) at   net.sf.json.util.newbeaninstancestrategy$ Defaultnewbeaninstancestrategy.newinstance (newbeaninstancestrategy.java:55) at   Net.sf.json.JSONObject.toBean (jsonobject.java:282) ...   2 more

Json-lib use

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.