0

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;
Roman C
  • 49,761
  • 33
  • 66
  • 176
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

1 Answers1

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,'')},
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It is not replacing [ and ] getting the same value – xrcwrn Aug 06 '15 at 09:26
  • @xrcwrn Listen, I give you an idea and you try it yourself, I can't test it in my environment, but I know that replacement should work, and if it doesn't then you should test it. You can also let me know what's wrong with it and I will update the answer. – Roman C Aug 06 '15 at 09:32
  • No, It's not ok, I need my answers to be upvoted and accepted. If you don't want to help me why should I help you? – Roman C Aug 06 '15 at 09:42
  • I tried `data: {ids: JSON.stringify(ids).replace(/[\[\]-]/g, "")},` Which is removing `[]` but still in server it is showing `ID ARE ["25","27","28"] data "25","27","28"` – xrcwrn Aug 06 '15 at 10:01
  • It's correct, the `ArrayList` uses `toString()` to print this. What else do you need? And it's suspicious escaping, using regex in java you need to escape `\\` character. – Roman C Aug 06 '15 at 10:09
  • then how it will show ids(0)=25; ids(1)=27; ids(2)=28; – xrcwrn Aug 06 '15 at 10:11
  • Whare do you want to show it, in console? `System.out.println("ids("+i+")="+ids.get(i)+";");` – Roman C Aug 06 '15 at 10:15
  • No I wat receceive it in ArrayList as id(0) value=25 ans similar – xrcwrn Aug 06 '15 at 11:06
  • I want received data should be populated in arrayList ids like ids(0)=25; ids(1)=27; ids(2)=28; – xrcwrn Aug 06 '15 at 11:07
  • I didn't get, isn't it printed to the console? – Roman C Aug 06 '15 at 11:14
  • at server side ` System.out.println("data " + a);` printing data "25","27","28" – xrcwrn Aug 06 '15 at 11:17
  • This is because elements of the list is of type string, try `List`. – Roman C Aug 06 '15 at 11:19
  • Not Working it is giving value as string – xrcwrn Aug 07 '15 at 04:54
  • I don't understand what are you saying. – Roman C Aug 07 '15 at 08:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85407/discussion-between-xrcwrn-and-roman-c). – xrcwrn Aug 07 '15 at 10:32