Reputation: 1457
Using jquery I am trying to read values from a json file and populate in a dropdown.
For that I am doing the following
but undefined
value is getting in dropdown.
What's wrong with my code?
json file
{
"VMs":[
{
"ID":"VM-WIN7-64",
"OS":"Windows 7",
"FLAVOUR":"VM-IE8-001-preq",
"ADAPTER":"Win 9"
}
],
"Paths":{
"VM-WIN7-64":"C:\\VirtualMachines\\Win7_X64_VM-001\\Win7_X64_VM-001.vmx"
}
}
code
function getOsNames() {
$.getJSON('/json/data.json', function(data)
{
var html = '';
$.each(data,function(val,i)
{
alert(val);
html += '<option value="' + val.OS + '">' + val.OS + '</option>';
});
$('#op').html(html);
});
}
Upvotes: 0
Views: 69
Reputation: 568
You can use for loop :
for(var i=0;i<data.VMs.length;i++)
{
html += '<option value="' + data.VMs[i].OS + '">' + data[i].VMs[i].OS + '</option>';
}
$('#op').html(html);
This will solve your problem
Upvotes: 0
Reputation: 388316
You have to loop through data.VMs
as it is having the OS property
function getOsNames() {
$.getJSON('/json/data.json', function (data) {
var html = '';
$.each(data.VMs, function (i, val) {
html += '<option value="' + val.OS + '">' + val.OS + '</option>';
});
$('#op').html(html);
});
}
Demo: Fiddle
Upvotes: 2