Json-lib can convert a Java object into a JSON-formatted string or convert a Java object into an XML-formatted document, either converting a JSON string into a Java object or converting an XML string into a Java object.
First, preparatory work
1, the first to go to the official Download Json-lib Toolkit
Download Address:
http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/
The latest is the 2.4 version, which is used in the example V2.3;json-lib also requires the following dependent packages:
Jakarta Commons-lang 2.5
Jakarta commons-beanutils 1.8.0
Jakarta commons-collections 3.2.1
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6
Official website: http://json-lib.sourceforge.net/
Then add the following jar package to the project:
Of course, you can also use the 2.4 json-lib library.
You can look at the official examples here:
Http://json-lib.sourceforge.net/usage.html
Because the examples in this presentation require JUnit tools, you will also need to add JUnit jar files, version 4.8.2 version, download address: https://github.com/KentBeck/junit/downloads
If you don't know what JSON is. Then you should be able to see http://www.json.org/json-zh.html.
2, the code to convert the JavaBean is as follows:
Package com.hoo.entity;
public class Student {
private int id;
private String name;
Private String Email;
Private String address;
private birthday birthday;
Setter, getter
Public String toString () {
return this.name + "#" + this.id + "#" + this.address + "#" + This.birthday + "#" + this.email;
}
}
Birthday.java
Package com.hoo.entity;
public class Birthday {
Private String birthday;
Public birthday (String birthday) {
Super ();
This.birthday = Birthday;
}
Setter, getter
Public birthday () {}
@Override
Public String toString () {
return this.birthday;
}
}
Notice that the getter and setter methods above are omitted and built by themselves.
3, the new Jsonlibtest test class, the basic code is as follows:
Package com.hoo.test;
Import java.lang.reflect.InvocationTargetException;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
import Net.sf.json.JSON;