JSON-to-Java Data conversion (JSON, or JavaScript Object natation, is a lightweight data interchange format that is ideal for server-to-javascript interaction. )
In the previous article, there is the package data in the background.
jsonobject Jo = jsonobject.fromobject (map);
Common Java code converted to JSON
1. The list collection is converted into JSON code
List List = new ArrayList ();
List.add ("first");
List.add ("second");
Jsonarray jsonArray2 = jsonarray.fromobject (list);
2. Map collection into 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 arrays into JSON code
boolean[] Boolarray = new boolean[] {true, false, true};
Jsonarray jsonArray1 = Jsonarray.fromobject (Boolarray);
5. Conversion of general data into JSON code
Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");
Reprinted from: http://blog.sina.com.cn/s/blog_821025b70100wh6v.html Thanks for sharing!
Jsonobject.fromobject (map) (conversion of JSON to Java data)