jsonlib的一些用法
1. List集合转换成json代码
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合转换成json代码
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转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );
使用时需要的包
commons-beanutils.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk15.jar
用jsonlib转了之后用tostring
chatroom.put("roomlist", roomlist.toString());
chatroom.put("memberlist", memberlist.toString());
JSONObject jo = JSONObject.fromObject(chatroom);
this.result = jo.toString();
return SUCCESS;
<package name="jsonaction" namespace="/" extends="json-default">
<action name="getlist" class="com.taozhi.action.ChatAction" method="getlist">
<result type="json"><param name="root">result</param></result>
</action>
</package>