如何实际解析接收到的json数据 [英] How to parse received json data in action
问题描述
我正在将JSP中的json数组数据发送到Action类,因为其中包含了struts2-json-plugin
.
我想解析它以作为ArrayList
元素接收它.
我的代码是
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
在JSP中:
$.ajax({
url: "UpdateNotification",
dataType: "json",
data: {ids: JSON.stringify(ids)},
success: function(data) {
alert("success " + data.st);
}
})
实际情况:
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);
}
}
}
运行时它正在显示数据
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;
推荐答案
Json stringify
方法返回json格式的值数组,例如["25","27","28"]
.但是要转换为列表,您需要删除[
或]
字符以提交CSV值.因为只能将CSV值转换为列表.试试
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,'')},
这篇关于如何实际解析接收到的json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!