This section focuses on using the Json-lib package to convert Java data types to JSON data types, that is, Java-->json.
First introduced Jsonserializer, which uses I simple clean interface way to convert any Java and JSON directly, Java-->json, using Jsonserializer.tojson (), Json-->java Use Tojava ().
1 // 2 String s1 = new String ("{1:1}" 3 string s2 = new String ("[All]" ); 4 jsonobject json1 = (Jsonobject) Jsonserializer.tojson (S1); 5 System.out.println ("--Converts a string with Jsonserializer json--" + Json1); 6 jsonarray json2 = (Jsonarray) Jsonserializer.tojson (S2); 7 System.out.println ("--convert string Jsonserializer with jsonarray--" + json2);
Show results
1 --convert string with Jsonserializer json--{"1": 1}2 --convert string with Jsonserializer jsonarray--[1,1]
Convert Java arrays and collections to JSON
1 //working with arrays and collections,—— >json2 Boolean[] Booleanarray =New Boolean[] {true,false,true };3Jsonarray Fromobject =Jsonarray.fromobject (booleanarray);4 System.out.println (fromobject);5 6List List =NewArrayList ();7List.add ("First");8List.add ("Second");9Jsonarray FromObject2 =jsonarray.fromobject (list);TenSystem.out.println (FROMOBJECT2);
Results
1 [true,false,true]2 ["First", "second"]
Convert beans and map to JSON
1 //Working with Objects2 //From Beans & Maps ToJSON3Map map =NewHashMap ();4Map.put ("name", "JSON");5Map.put ("bool", boolean.true);6Map.put ("int",NewInteger (1));7Map.put ("Arr",NewString[] {"A", "B" });8Map.put ("func", "function (i) {return this.arr[i];}");9 TenJsonobject JsonObject2 =jsonobject.fromobject (map); OneSystem.out.println ("--map conversion json--\n" + jsonObject2);
Results
1 --map Conversion json-- 2 return this. Arr[i]; }, "bool":True}
The beans transformation first customizes the object Mybean,
1 Public classMybean {2 3 PrivateString name = "JSON";4 Private intPojoid = 1;5 Private Char[] options =New Char[] {' A ', ' F ' };6 PrivateString func1 = "function (i) {return this.options[i];}";7 PrivateJsonfunction Func2 =NewJsonfunction (NewString[] {"I" },8"Return this.options[i];");9 //Custom TypesTen PrivateMyBean2 mybean2 =NewMyBean2 ("1", "Mybean"); One A PublicString GetName () { - returnname; - } the Public voidsetName (String name) { - This. Name =name; - } - Public intgetpojoid () { + returnpojoid; - } + Public voidSetpojoid (intpojoid) { A This. pojoid =pojoid; at } - Public Char[] getoptions () { - returnoptions; - } - Public voidSetOptions (Char[] options) { - This. options =options; in } - PublicString getFunc1 () { to returnfunc1; + } - Public voidsetFunc1 (String func1) { the This. func1 =func1; * } $ Publicjsonfunction GetFunc2 () {Panax Notoginseng returnFunc2; - } the Public voidSetFunc2 (jsonfunction func2) { + This. Func2 =Func2; A } the PublicMyBean2 getMybean2 () { + returnmybean2; - } $ Public voidsetMybean2 (MyBean2 mybean2) { $ This. mybean2 =mybean2; - } -}
The Mybean contains custom objects Mybean2
Public classMyBean2 {PrivateString ID; PrivateString name; PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicMyBean2 (string ID, string name) {Super(); This. ID =ID; This. Name =name; } PublicMyBean2 () {Super(); }}
Convert Beans to JSON
1 // The custom Mybean object contains the MyBean2 object, so bean-->json, whether or not a custom object, can convert the Json format 2 Jsonobject jsonObject3 = Jsonobject.fromobject (new Mybean ()); 3 System.out.println ("--beans conversion json--\n" + jsonObject3);
Results
1 --beans Conversion json-- 2 return This return this. Options[i]; }, "Options": ["a", "F"]}
Summary: Convert Java objects to JSON, relatively simple, if beans or maps with jsonobject.fromobject (), if an array or collections, with Jsonarray.fromobject ().
A brief introduction to the--json-lib of Json experience (II.)