首先前台调用: $("#json").bind("click",function(){
var jsons = {userList:[{"age":88,"email":"[email protected]","id":70,"keshi":"骨 科","passWord":"feiji","phoneNumber":"34234","sex":1,"userId":"23432432","userName":"feiji","userType":1}]};
//alert(para);
$.ajax({
type:"POST",
url:"jsontest",
datatype:"json",
data:jsons,
success:function(msg){
alert(msg.userList[0].userName);
}
});
});后台: private List<User> userList; public List<User> getUserList() {
return userList;
} @JSON(serialize = true, deserialize = true)
public void setUserList(List<User> userList) {
this.userList = userList;
}
public String execute() {
// this.name = "feiji";
// System.out.println("========" + name + "=======");
// this.name = "daifei";
System.out.println("userList size:" + userList.size()
+ "**************");
System.out.println(userList.get(0).getUserName()
+ "$$$$$$$$$$$$$$$$$$$$$$");
return SUCCESS;
}struts 配置: <package name="default" namespace="/" extends="json-default">
<default-action-ref name="hello"></default-action-ref>
<action name="jsontest" class="com.controller.JsonTest">
<result type="json">
<param name="includeProperties">userList\[\d\]\.userName</param>
</result>
</action>执行后控制台输出: [DEBUG] 2012-04-10 10:45:51 :Executing action method = null
userList size:1**************
null$$$$$$$$$$$$$$$$$$$$$$ [DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList
[DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList\[\d\]
[DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList\[\d\]\.userName
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: name
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: user
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].age
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].email
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].id
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].keshi
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].passWord
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].phoneNumber
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].sex
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].userId
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].userType
[DEBUG] 2012-04-10 10:45:51 :[JSON]{"userList":[{"userName":null}]}
也就是说数据json数据没有封装到userList 中去,我调试了好就找不到原因
var jsons = {userList:[{"age":88,"email":"[email protected]","id":70,"keshi":"骨 科","passWord":"feiji","phoneNumber":"34234","sex":1,"userId":"23432432","userName":"feiji","userType":1}]};
//alert(para);
$.ajax({
type:"POST",
url:"jsontest",
datatype:"json",
data:jsons,
success:function(msg){
alert(msg.userList[0].userName);
}
});
});后台: private List<User> userList; public List<User> getUserList() {
return userList;
} @JSON(serialize = true, deserialize = true)
public void setUserList(List<User> userList) {
this.userList = userList;
}
public String execute() {
// this.name = "feiji";
// System.out.println("========" + name + "=======");
// this.name = "daifei";
System.out.println("userList size:" + userList.size()
+ "**************");
System.out.println(userList.get(0).getUserName()
+ "$$$$$$$$$$$$$$$$$$$$$$");
return SUCCESS;
}struts 配置: <package name="default" namespace="/" extends="json-default">
<default-action-ref name="hello"></default-action-ref>
<action name="jsontest" class="com.controller.JsonTest">
<result type="json">
<param name="includeProperties">userList\[\d\]\.userName</param>
</result>
</action>执行后控制台输出: [DEBUG] 2012-04-10 10:45:51 :Executing action method = null
userList size:1**************
null$$$$$$$$$$$$$$$$$$$$$$ [DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList
[DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList\[\d\]
[DEBUG] 2012-04-10 10:45:51 :Adding include property expression: userList\[\d\]\.userName
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: name
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: user
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].age
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].email
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].id
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].keshi
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].passWord
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].phoneNumber
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].sex
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].userId
[DEBUG] 2012-04-10 10:45:51 :Ignoring property because of include rule: userList[0].userType
[DEBUG] 2012-04-10 10:45:51 :[JSON]{"userList":[{"userName":null}]}
也就是说数据json数据没有封装到userList 中去,我调试了好就找不到原因
{
;
private List<User> users;ViewJson是json,User也是json...