0

In my code I am creating an array-list in JSON and want to response this arraylist into jquery.

List<String> list = new ArrayList<String>();
for(int i=0;i<5;i++){ 
list.add(i);
}

How can I response this list to my jquery file and get retrieve it in jquery? For a simple String I know how to response and retrieve in jquery using ajax. For example:- IN JSON

String name ="abcd";
writer.key("nameofperson").value(name);

and in jquery

  $.ajax({
                url: baseURL + '.json', 
                dataType: 'json',
                type:'POST',
                success: function(data)
                    {
                        console.log("test sucess");
                        var ROI =data['nameofperson'];
                    alert(ROI)   
            },

It is working. How can i acheive the same for arraylist?

1
  • There seems to be Java in your example, not that Javascript is different than Java. Also there seems to be confusion around running code vs. files storing code. Commented Dec 17, 2013 at 8:29

1 Answer 1

0

It's hard to answer without sample json , but in general - this is what you need to do :

System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
var myJson = js.Serialize(list);

and do this in client side :

 success: function(data)
                    {

                      for( var i in data )
                          {
                              alert(data[i])
                          }
                   }
Sign up to request clarification or add additional context in comments.

Comments

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.