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!

Mr. C
  • 1,652
  • 2
  • 16
  • 22
  • I believe this is not valid , valid JSON will be something like {"key:“2”, "key":“3”, "key":“7”, "key":“14”} – Satya Sep 28 '13 at 03: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 – MayThrow Sep 28 '13 at 03:54

6 Answers6

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.

Blaise
  • 21,314
  • 28
  • 108
  • 169
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)
          );
     });
 });
Community
  • 1
  • 1
Mr. C
  • 1,652
  • 2
  • 16
  • 22
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); 
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)
        );
        });
   }
Deepak Kothari
  • 1,601
  • 24
  • 31
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>'));
 });`
-4

For me, this one worked.

$("#ddlCaseType").append("<option>" + txt + "</option>");
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131