spring 与dwr整合见 http://blog.csdn.net/buyaore_wo/archive/2010/09/20/5897529.aspx
java端json与javabean对象转换用json-lib插件 见 http://blog.csdn.net/buyaore_wo/archive/2010/09/27/5910174.aspx
js端json与javascript对象转换用json2框架 下载 http://www.json.org/js.html
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,减肥的xml,易于阅读和编写,同时也易于机器解析和生成。它基于ECMA262语言规范(1999-12第三版)中JavaScript编程语言 的一个子集。JSON采用与编程语言无关的文本格式,但是也使用了类C语言(包括C, C++, C#, Java, JavaScript, Perl, Python等)的习惯,这些特性使JSON成为理想的数据交换格式。
示例代码:
先来一下json序列与javabean对象间转换的基类:
package com.test.json; import java.util.Collection; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public abstract class JSONSurppot { /** * java对象转换为JSON对象 * * @param obj * @return */ public JSON toJSON(Object obj) { return JSONSerializer.toJSON(obj); } /** * java对象转换为json序列串 * * @param obj * @return */ public String toJSONString(Object obj) { return toJSON(obj).toString(); } /** * json序列串转换为javabean * * @param jsonString * @param clazz * 要转换的目标javabean的类型 * @return */ public Object toBean(String jsonString, Class<?> clazz) { JSONObject jsonObject = JSONObject.fromObject(jsonString); return JSONObject.toBean(jsonObject, clazz); } public JSONArray toJSONArray(String jsonString) { return JSONArray.fromObject(jsonString); } /** * json序列串转换为javaean数组 * * @param jsonString * @return */ public Object toJavaArray(String jsonString) { return JSONArray.toArray(toJSONArray(jsonString)); } /** * json序列串转换为指定类型(相应的)javabean数组 * * @param jsonString * @param clazz * @return */ public Object toJavaArray(String jsonString, Class<?> clazz) { return JSONArray.toArray(toJSONArray(jsonString), clazz); } /** * json序列串转换为javaean集合 * * @param jsonString * @return */ public Collection<?> toCollection(String jsonString) { return JSONArray.toCollection(toJSONArray(jsonString)); } /** * json序列串转换为指定类型(相应的)javabean集合 * * @param jsonString * @param clazz * @return */ public Collection<?> toCollection(String jsonString, Class<?> clazz) { return JSONArray.toCollection(toJSONArray(jsonString), clazz); } }
再来个测试使用类,继承自上面那个类
package com.test; import java.util.Collection; import com.test.json.JSONSurppot; public class Test extends JSONSurppot { public String testCollection(String jsonString) { System.out.println("---------" + jsonString); Collection<User> users = (Collection<User>) toCollection(jsonString, User.class); for (User user : users) { System.out.println(user.getName()); } return toJSONString(new User("pan", "123546")); } public String testObject(String jsonString) { System.out.println("---------" + jsonString); User user = (User) toBean(jsonString, User.class); System.out.println(user.getName() + "," + user.getPassword()); return toJSONString(new User("pan", "123546")); } }
spring中dwr暴露
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"> <bean id="test" class="com.test.Test"> <dwr:remote javascript="TestDwr"> <dwr:include method="testCollection" /> <dwr:include method="testObject" /> </dwr:remote> </bean> </beans>
dwr配置见
http://blog.csdn.net/buyaore_wo/archive/2010/09/20/5897529.aspx
客户端使用
<mce:script type="text/javascript" src="./dwr/engine.js" mce_src="dwr/engine.js"></mce:script> <mce:script type="text/javascript" src="./dwr/interface/TestDwr.js" mce_src="dwr/interface/TestDwr.js"></mce:script> <mce:script type="text/javascript" src="./dwr/util.js" mce_src="dwr/util.js"></mce:script> <mce:script type="text/javascript" src="./json2.js" mce_src="json2.js"></mce:script> <mce:script type="text/javascript"><!-- temp = []; var user1 = {}; user1.name = "pan"; user1.password = "78987"; temp.push(user1); var user2 = {}; user2.name = "pawern"; user2.password = "789fghdfh87"; temp.push(user2); //传递参数为对象数组 TestDwr.testCollection(JSON.stringify(temp), { callback : function(data) { alert(JSON.parse(data).name); } }); //传递参数为单个对象 TestDwr.testObject(JSON.stringify(user1), { callback : function(data) { alert(JSON.parse(data).name); } }); // --></mce:script>
示例代码下载(myeclispe工作空间):http://pansexamples.googlecode.com/files/springdwr.rar