JSON is a very simple and important data format, which is usually used to exchange and transmit data. It is widely used in JavaScript technology and is becoming increasingly popular in various popular programming languages. There is also a JSON library in Java, which is used to convert Java objects to JSON and XML data and has an important application.
Environment: JDK5, json-lib-2.3-jdk15
Dependent packages: json-lib-2.3-jdk15.jar, commons-collections.jar, commons-lang. jar, commons-logging.jar, commons-beanutils.jar, ezmorph-1.0.6.jar, xom-1.1.jar
Json format corresponding to various types in java:
1. array or set --> JSON string
Public static void test1 (){
System. out. println ("------------ array or set --> JSON string ----------");
Boolean [] boolArray = new boolean [] {true, false, true };
JSONArray jsonArray1 = JSONArray. fromObject (boolArray );
System. out. println (jsonArray1 );
// Output format: [true, false, true]
List list = new ArrayList ();
List. add ("first ");
List. add ("second ");
JSONArray jsonArray2 = JSONArray. fromObject (list );
System. out. println (jsonArray2 );
// Output format: ["first", "second"]
JSONArray jsonArray3 = JSONArray. fromObject ("[json, is, easy]");
System. out. println (jsonArray3 );
// Output format: ["json", "is", "easy"]
}
2. Object | Map --> JSON string
Public static void test2 (){
System. out. println ("------------ Object | Map --> JSON 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];}");
JSONObject jsonObject1 = JSONObject. fromObject (map );
System. out. println (jsonObject1 );
// Output format: {"func": function (I) {return this. arr [I] ;}, "arr": ["a", "B"], "int": 1, "bool": true, "name ": "json "}
JSONObject jsonObject2 = JSONObject. fromObject (new MyBean ());
System. out. println (jsonObject2 );
// Output format: {"func1": function (I) {return this. options [I];}, "func2": function (I) {return this. options [I];}, "name": "json", "options": ["a", "f"], "pojoId": 1}
}
Public 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];");
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getPojoId (){
Return pojoId;
}
Public void setPojoId (int pojoId ){
This. pojoId = pojoId;
}
Public char [] getOptions (){
Return options;
}
Public void setOptions (char [] options ){
This. options = options;
}
Public String getFunc1 (){
Return func1;
}
Public void setFunc1 (String func1 ){
This. func1 = func1;
}
Public JSONFunction getFunc2 (){
Return func2;
}
Public void setFunc2 (JSONFunction func2 ){
This. func2 = func2;
}
}
3. JSON string --> Object
Public static void test3 (){
System. out. println ("------------ JSON string --> Object ----------");
String json1 = "{name =" json ", bool: true, int: 1, double: 2.2, func: function (a) {return a ;}, array: [1, 2]} ";
JSONObject jsonObject1 = JSONObject. fromObject (json1 );
Object bean1 = JSONObject. toBean (jsonObject1 );
System. out. println (bean1 );
// Net. sf. ezmorph. bean. MorphDynaBean @ 10dd1f7 [
// {Double = 2.2, func = function (a) {return a;}, int = 1, name = json, bool = true, array = [1, 2]}
//]
String json2 = "{bool: true, integer: 1, string:" json "}";
JSONObject jsonObject2 = JSONObject. fromObject (json2 );
BeanA bean2 = (BeanA) JSONObject. toBean (jsonObject2, BeanA. class );
System. out. println (bean2 );
// BeanA {bool = true, integer = 1, string = json}
}
Public class BeanA {
Private boolean bool;
Private Integer;
Private String string;
Public boolean isBool (){
Return bool;
}
Public void setBool (boolean bool ){
This. bool = bool;
}
Public Integer getInteger (){
Return integer;
}
Public void setInteger (Integer integer ){
This. integer = integer;
}
Public String getString (){
Return string;
}
Public void setString (String string ){
This. string = string;
}
@ Override
Public String toString (){
Return "BeanA {" bool = "+ bool +", integer = "+ integer +", string = "+ string ++ "}";
}
}
4. JSON string --> XML
Public static void test4 (){
System. out. println ("------------ JSON string --> XML ----------");
JSONObject json = new JSONObject (true );
String xml = new XMLSerializer (). Write (json );
System. out. println (xml );
JSONObject json1 = JSONObject. fromObject ("{" name ":" json "," bool ": true," int ": 1 }");
String xml1 = new XMLSerializer (). Write (json1 );
System. out. println (xml1 );
JSONArray json2 = JSONArray. fromObject ("[1, 2, 3]");
String xml2 = new XMLSerializer (). Write (json2 );
System. out. println (xml2 );
}
5. XML --> JSON string
Public static void test5 (){
System. out. println ("------------ XML --> JSON string ----------");
String xml = "" +
"<A class =" array ">" +
"<E type =" function "params =" I, j ">" +
"Return matrix [I] [j];" +
"</E>"