9

Let's say I have this data returned from a backend service layer:

["2", "3", "7", "14"]

First question. Is this considered valid JSON data? Json.org says it is, but I cannot find any references to it on Google, SO, etc...

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

I want to be able to take these values and add them to an already existing DropDownList object OnLoad using JQuery.

$.getJSON("http://www.example.com/path-to-get-data", function(data) { 
  //iterate through each object and add it to the list.
  //ideally, the key and value (string to display to the end user) would be the same.
});

I took a look at this thread, but it has objects, not just an array. Should I use parseJSON versus getJSON?

Thanks in advance!

2
  • I believe this is not valid , valid JSON will be something like {"key:“2”, "key":“3”, "key":“7”, "key":“14”} Commented Sep 28, 2013 at 3:33
  • $.parseJSON('["2", "3", "7", "14"]') will return an array. Better use getJSON and iterate through each item in the array and add it to your object Commented Sep 28, 2013 at 3:54

6 Answers 6

13
var arr = [ 2, 3, 7, 14];
$.each(arr, function(index, value) {
     ('#myselect').append($('<option>').text(value).attr('value', index));
});

Please also take a look at Mr. C's solution:

$('<option></option>').val(item).html(item)

His way of manipulating options is new to me, and more elegant.

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

4 Comments

It threw "Object doesnot support .append"
What is your version of jQuery and what is your browser?
$.append is available since very early version. Is your DOM selector (that $('#myselect') part) right?
There's a small typo on line 3 of this answer, that I caught. The $ in front of ('#myselect).append($('<option>').text(value).attr('value', index)); is missing. This was very helpful, thanks.
11

My solution was based off of Blaise's idea.

//populate the Drop Down List
$.getJSON("http://www.example.com/api/service", function (data) {
     $.each(data, function (index, item) {
         $('#dropDownList').append(
              $('<option></option>').val(item).html(item)
          );
     });
 });

Comments

2

This is not correct JSON, it must be this style:

{"0": "2", "1" : "3", "2" : "7", "3" : "14"}

You could use that sample to procceed you response:

var response = "[2, 3, 7, 14]";
eval("var tmp = " + response); 
console.log(tmp); 

Comments

1

This is the HiddenField declaration,which is useful to Store The JSON String

   <asp:HiddenField ID="hdnBankStatus" runat="server" />

This is the Dropdownlist declaration

   <select id="optStatus" name="optStatus" runat="server">
        </select> 

These lines will initialize Hiddenfield value with the sorted list(Assume that list contains the key value pairs in sorted order) which is then serialized using JSON serializer

   sl = new SortedList();
   hdnBankStatus.Value = jsonSerialiser.Serialize(sl);

These lines will use Elements of JSON String One by one and populate the dropdown with Values.

   $(document).ready(function () {  

   var myOptions = $.parseJSON($('#hdnBankStatus').val());
        var mySelect = $('#optStatus');
        $.each(myOptions, function (val, text) {
            mySelect.append(
    $('<option></option>').val(text).html(val)
        );
        });
   }

Comments

0
 var arr = [ 2, 3, 7, 14];
 var dropdown = ('#myselect');
 dropdown.empty();
 dropdown.append($('<option value=''>--Please Select--</option>'));
 $.each(arr, function(index, value) {
 dropdown.append($('<option value='+index+'>'+value+'</option>'));
 });`

1 Comment

This should do it
-4

For me, this one worked.

$("#ddlCaseType").append("<option>" + txt + "</option>");

1 Comment

$("#ddlCaseType").append("<option>" + txt + "</option>");

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.