Java code converted to JSON code
1. List collection converted to JSON code
List List = new ArrayList (); List.add ("a"); List.add ("second"); Jsonarray jsonArray2 = jsonarray.fromobject (list); |
2. Map collection converted to JSON code
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 JSON = jsonobject.fromobject (map); |
3. Bean converted to JSON code
Jsonobject jsonobject = jsonobject.fromobject (New Jsonbean ()); |
4. Convert array into JSON code
Boolean [] Boolarray = new Boolean[] {true,false,true }; Jsonarray jsonArray1 = Jsonarray.fromobject (Boolarray); |
5. Convert general data into JSON code
Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']"); |
6. Convert Beans to JSON code
List List = new ArrayList (); JsonBean2 jb1 = new JsonBean2 (); Jb1.setcol (1); Jb1.setrow (1); Jb1.setvalue ("xx"); JsonBean2 jb2 = new JsonBean2 (); Jb2.setcol (2); Jb2.setrow (2); Jb2.setvalue (""); List.add (JB1); List.add (JB2); Jsonarray ja = jsonarray.fromobject (list); |