1,数组,集合转换为JSON
如果要将数组,集合转换为JSON,那么可以JSONArray.fromObject()方法来实现,同时也可以使用JSONSerializer.toJSON()来做到。
// 数组 boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); assertEquals("[true,false,true]", jsonArray.toString()); // 集合 List<String> list = new ArrayList<String>(); list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); assertEquals("[\"first\",\"second\"]", jsonArray.toString());
JSONSerializer.toJSON()方法的例子
// 数组 boolean[] boolArray = new boolean[] { true, false, true }; JSON json = JSONSerializer.toJSON(boolArray); assertEquals("[true,false,true]", json.toString()); // 集合 List<String> list = new ArrayList<String>(); list.add("first"); list.add("second"); JSON json = JSONSerializer.toJSON(list); assertEquals("[\"first\",\"second\"]", json.toString());
2,Map,JAVA BEAN转换为JSON
如果要将Map,JAVA BEAN转换为JSON,那么可以JSONObject.fromObject()方法来实现,同时也可以使用JSONSerializer.toJSON()来做到。
//MAP Map<String, Object> map = new HashMap<String, Object>(); 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 jsonObject = JSONObject.fromObject(map); assertEquals( "{\"arr\":[\"a\",\"b\"],\"int\":1,\"name\":\"json\",\"func\":function(i){ return this.arr[i]; },\"bool\":true}", jsonObject.toString()); //JAVA BEAN User user = new User(); user.setName("Jimmy Liu"); user.setAge(28); user.setAddresses(new String[] { "shenzhen,changsha" }); User friend = new User(); friend.setName("Eric Liu"); friend.setAge(30); friend.setAddresses(new String[] { "shenzhen,nanchange" }); List<User> friends = new ArrayList<User>(); friends.add(friend); user.setFriends(friends); JSONObject jsonObject = JSONObject.fromObject(user); assertEquals( "{\"friends\":[{\"friends\":[],\"age\":30,\"name\":\"Eric Liu\",\"addresses\":[\"shenzhen,nanchange\"],\"func1\":function(data){ alert(data); },\"func2\":function(data){ return data; }}],\"name\":\"Jimmy Liu\",\"age\":28,\"addresses\":[\"shenzhen,changsha\"],\"func1\":function(data){ alert(data); },\"func2\":function(data){ return data; }}", jsonObject.toString());
3,JSON转换为动态BEAN,特定BEAN
如果要将JSON转换为动态BEAN,特定BEAN,那么可以JSONObject.toBean()方法来实现
//转换为动态BEAN String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}"; JSONObject jsonObject = JSONObject.fromObject(json); Object bean = JSONObject.toBean(jsonObject); assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name")); assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool")); assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int")); assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double")); assertEquals(jsonObject.get("func"), PropertyUtils.getProperty(bean, "func")); List expected = JSONArray.toList(jsonObject.getJSONArray("array")); assertEquals(expected.size(), ((List) PropertyUtils.getProperty(bean, "array")).size()); //转换为特定BEAN String json = "{\"friends\":[],\"name\":\"Jimmy Liu\",\"age\":28,\"addresses\":[\"shenzhen,changsha\"],\"func1\":function(data){ alert(data); },\"func2\":function(data){ return data; }}"; JSONObject jsonObject = JSONObject.fromObject(json); User user = (User) JSONObject.toBean(jsonObject, User.class); assertEquals(jsonObject.get("name"), user.getName()); assertEquals(jsonObject.get("age"), user.getAge()); assertEquals(jsonObject.get("func1"), user.getFunc1()); List expected = JSONArray.toList(jsonObject.getJSONArray("addresses")); assertEquals(expected.size(), user.getAddresses().length); // 在JSON转换为Bean时,如果bean包括集合的属性且集合包括其他JAVA BEAN,默认情况下,JSONLIB将转为动态BEAN json = "{'roleName':'administrator','privilegeList':[{'privilegeName':'system-manager'},{'privilegeName':'supplier-manager'}]}"; Map<String, Object> classMap = new HashMap<String, Object>(); classMap.put("privilegeList", Privilege.class); Role role = (Role) JSONObject.toBean(JSONObject.fromObject(json), Role.class, classMap); jsonObject = JSONObject.fromObject(json); assertEquals(jsonObject.get("roleName"), role.getRoleName()); List privilegeList = JSONArray.toList(jsonObject .getJSONArray("privilegeList")); assertEquals(privilegeList.size(), role.getPrivilegeList().size()); assertEquals(PropertyUtils.getProperty(privilegeList.get(0), "privilegeName"), role.getPrivilegeList().get(0) .getPrivilegeName());
4,JSON TO XML
可以利用XMLSerializer.write()方法将JSON转化为XML,同时利用的Setter方法控制XML的输出,比如,你能够改变XML的根元素,你可以注册,管理namespaces ,还可以向XML增加属性。
//Boolean JSONObject json = new JSONObject(true); String xml = new XMLSerializer().write(json); System.out.println(xml); //Object JSONObject json = JSONObject .fromObject("{\"privilegeName\":\"system-manager\"}"); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setObjectName("privilege"); String xml = xmlSerializer.write(json); System.out.println(xml); //Array JSONArray json = JSONArray.fromObject("[1,2,3]"); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setArrayName("array"); xmlSerializer.setElementName("element"); String xml = xmlSerializer.write(json); System.out.println(xml);
5,XML To JSON
XMLSerializer.read()方法将XML转化为JSON
JSONObject json = (JSONObject) new XMLSerializer() .read("<?xml version=\"1.0\" encoding=\"UTF-8\"?><o><privilegeName type=\"string\">system-manager</privilegeName></o>" ); System.out.println( json );
6,测试用到的JAVA BEAN,去除了Getter,Setter方法
public class User { private String name; private Integer age; private String[] addresses; private List<User> friends; private String func1 = "function(data){ alert(data); }"; private JSONFunction func2 = new JSONFunction(new String[] { "data" }, "return data;"); } public class Role { private String roleName; private List<Privilege> privilegeList; } public class Privilege { private String privilegeName; }
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>xom</groupId> <artifactId>xom</artifactId> <version>1.1</version> </dependency>