0

让我解释一下当前状态是什么,然后我会提到这个问题。

在我的 jsp 页面中,我正在通过javascript 中$getJSON的方法调用 Struts 2 Action。jQuery

这是代码:

var a = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};
var b = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};
var c = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};

var users = [a,b,c];

   $.getJSON
      (
        "createUser.action",
        {
            "users":users+""
        },
        function (data){
//          alert(data.status);
            if(data.status == "SUCCESS"){
                location.reload();              
            }else{
                alert("Creating User Failed");
            }   
        }
    );  

我想在我的 Struts Action 类中读取通过请求发送的 json 对象。这是我的动作课:

public class ProcessUser extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;

@Override
public void setServletRequest(HttpServletRequest servletRequest) {
    // TODO Auto-generated method stub
    this.request = servletRequest;
}

public String execute() {


    String data = request.getParameter("users");

    System.out.println("data is :"+data);


    //data = "{\"users\":"+data+"}";

    System.out.println("data is :"+data);


    //data = "{\n  \"users\": [\n    {\n      \"id\": \"ddsa\",\n      \"firstName\": \"dasda\",\n      \"lastName\": \"asaas\",\n      \"email\": \"dasdds\"\n    },\n    {\n      \"id\": \"ddsa\",\n      \"firstName\": \"dasda\",\n      \"lastName\": \"asaas\",\n      \"email\": \"dasdds\"\n    }\n  ]\n}";

    try {
        JSONObject json = (JSONObject)new JSONParser().parse(data);

        JSONArray jsonArray = (JSONArray)json.get("users");

        JSONObject jsonObject = (JSONObject)jsonArray.get(0);

        System.out.println("id:"+jsonObject.get("firstName"));
        /*
        System.out.println("name=" + json.get("users[id"));
        System.out.println("width=" + json.get("users.firstName"));*/
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
 return SUCCESS;
 }

我得到一个像这样的异常堆栈跟踪:

Unexpected character (o) at position 1.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)

你能帮我解决这个问题吗?我怎样才能读取那个 json 对象。

4

2 回答 2

0

尝试以下代码来声明数组

JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input );

其中输入是 JSON 字符串

于 2013-08-06T07:05:50.603 回答
0

经过多次迭代,我找到了问题的解决方案。

从 JavaScript 发送请求时,我必须使用JSON.stringify(data),即

var a = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};
var b = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};
var c = {"id":"ddsa","firstName":"dasda","lastName":"asaas","email":"dasdds"};

var users = [a,b,c];

$.getJSON
  (
    "createUser.action",
    {
        "users":JSON.stringify(users)
    },
    function (data){
//          alert(data.status);
        if(data.status == "SUCCESS"){
            location.reload();              
        }else{
            alert("Creating User Failed");
        }   
    }
);  

在我使用的struts Action类中,

JSONArray jsonArray = (JSONArray) new JSONParser().parse(data);

然后迭代这个数组并JOSNObject获取所有的属性值,即

JOSNObject jsonObject = (JSONObject) jsonArray.get(i);

谢谢你的支持。

于 2013-08-06T09:54:35.110 回答