How to parse received json data in action

女生的网名这么多〃 提交于 2019-12-12 02:14:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!