让我解释一下当前状态是什么,然后我会提到这个问题。
在我的 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 对象。