1

In my Java script I'm create array and now I'm converting to Json Object using stringify method.

 <script type="text/javascript">
   var array1 = new Array();
    $.ajax({
        url : "Result",
        type : 'POST',
        data : {
            "array1" : JSON.stringify(array1),
            "globalClassId" : globalClassId
        }});
</script>

But in server side array1 parameter creating like ["1","2","3"] But we don't want extra bracket []

How to Remove this extra bracket?

If we remove this extra [] the code look like "1","2","3" , we can simple loop it.

2
  • 1
    didn't you just asked the same question a while ago stackoverflow.com/questions/17646724/… Commented Jul 15, 2013 at 5:28
  • 1
    Try array1.join() instead of JSON.stringify(array1) Commented Jul 15, 2013 at 5:31

3 Answers 3

0

Following code working for me..

  <script type="text/javascript">
       var array1 = new Array();
        $.ajax({
            url : "Result",
            type : 'POST',
            data : {
                "array1" : array1.join(),
                "globalClassId" : globalClassId
            }});
    </script>
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't escape special characters such as " or ,.
Also, I don't think this adds the double quotes as shown in your expected output.
0

Replace

JSON.stringify(array1)

With

JSON.stringify(array1).splice(1, -1)

Keep in mind that you might as well re-add the square brackets on the server-side in order to parse the results correctly as JSON. If you're wanting to use custom logic to interpret the data, make sure to correctly handle escaped characters.

Comments

-1

I was having the same problem, So used the following code...Hope it will suffix your requirement.

JSONObject obj = new JSONObject(request.getParameter("names"));

List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){

}

1 Comment

You should post an answer that us js not java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.