首先前台调用: $("#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 中去,我调试了好就找不到原因

解决方案 »

  1.   

    这个字符串,可以换一种思路实现试试,var jsons = {userList:[{"age":88,"email":"[email protected]","id":70,"keshi":"骨 科","passWord":"feiji","phoneNumber":"34234","sex":1,"userId":"23432432","userName":"feiji","userType":1}]};先把该字符串传到后台,再遍历,一个一个地封装成对象,然后再加到list里面。
     
      

  2.   

    就算该方法可行,但是若果这个json对象的数据超过一个呢,比如这个jsons里面包含了两个user的信息怎么办?
      

  3.   

    你可以定义一个json对象来封装列表jsonpublic class ViewJson
    {
    ;
    private List<User> users;ViewJson是json,User也是json...