问题
I am sending json array data from JSP to Action class for that I have included struts2-json-plugin
.
I want to parse it to receive it as a ArrayList
elements.
my code is
In JSP:
$.ajax({
url: "UpdateNotification",
dataType: "json",
data: {ids: JSON.stringify(ids)},
success: function(data) {
alert("success " + data.st);
}
})
In action:
public class Test extends ActionSupport {
ArrayList<String> ids = new ArrayList<String>();
public ArrayList<String> getIds() {
return ids;
}
public void setIds(ArrayList<String> ids) {
this.ids = ids;
}
public String updateNotification() {
System.out.println("ID ARE " + getIds());
for (String a : getIds()) {
System.out.println("data " + a);
}
}
}
On running it is showing data
ID ARE [["25","27","28"]]
data ["25","27","28"]
How can I get one data as one array element in action.
EDIT I am trying to get data as
ids(0)=25;
ids(1)=27;
ids(2)=28;
回答1:
Json stringify
method returns an array of values in json format something like ["25","27","28"]
. But to convert to a list you need to remove [
or ]
characters to submit CSV values. Because only CSV values could be converted to a list. Try
data: {ids: JSON.stringify(ids).replace(/[\[\]]/g,'')},
来源:https://stackoverflow.com/questions/31830042/how-to-parse-received-json-data-in-action