0

I want to know how to iterate a JSON object using jQuery. My requirement is I am getting a list from a Java Servlet to the UI and I have to populate a combo box with the AJAX response.

The above tack I already did it using struts2 and jQuery. Now I am in the middle of nowhere, how to iterate the Java List back in JSP:

$("#XXX option").remove(); 
$.each(data.YYYList, function(index, item) {
    $("#XXX").append($("<option></option>").text(item).val(item));
});

I have set the MIME type as response.setContentType("application/json");

Can any one please guide me how to achieve this. Please let me know if any other information is needed from me.

3
  • Please include a sample of the JSON text you want to iterate over, and what you need to do with it as you iterate over it (do you want to store it somewhere else? display it? display parts?). Your question is not clear. Also, if this is all client-side, remove references to java, JSP or struts2, as they are irrelevant. Commented Sep 13, 2012 at 13:49
  • sorry for the poor post : i want the data to be populated in a combo box , the values for the combo box will be coming from the database , Commented Sep 13, 2012 at 13:52
  • Nobody can help you unless you provide a sample input and sample output. Otherwise, we would only be able to teach you some JQuery... and there's a page for that (JQuery has excellent documentation). Commented Sep 13, 2012 at 13:53

2 Answers 2

2

Based on the small amount of information given by Esh, here is an example that I created for the very function you listed. I have a JSON that I want to be used in multiple select boxes.

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Example Json:

  "yyyList": [
    {
      "Id": "1",
      "Name": "aaaa "
    }, {
      "Id": "2",
      "Name": "bbb "
    }, {
      "Id": "6",
      "Name": "ccc "
    }, {
      "Id": "7",
      "Name": "ddd "
    } ]


$.ajax({
              url: "URL",
               //data: "",
               type: "GET",
               dataType: 'json',
               success: function (data) {            
               $.each(data.YYYList, function () {
                  $('#state').append('<'option value='+this.Id+'>'+this.Name+'<'option>');
               });
            }
        })

$('#state') ---> gives the same id for select tag in HTML Please make correct it option syntax

Hope this helps.

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.