I want to parse a JSON object and create drop downs out of it. There will be six drop downs -
productType reportNames startDate startMonth endDate endMonth
I am able to create the first drop down 'productType' but when I am trying to create others it shows me some Undefined error message.
Here is my code -
HTML
<select id="product_type"></select>
<select id="report_type"></select>
<select id="startDate"></select>
<select id="startMonth"></select>
<select id="endDate"></select>
<select id="endMonth"></select>
Javascript
var jsonString = '[{"productType": "ABC","reportNames": ["Report1",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate":["2010","2011"],"endMonth": ["May","June"]},"Report 2",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate": ["2010","2011"],"endMonth": ["May","June"]}]},{"productType": "XYZ","reportNames": ["Report 3",{"startDate": ["2010","2011"],"startMonth": ["May","June"],"endDate": ["2010","2011"],"endMonth": ["May","June"] },"Report 4",{"startDate": ["2010","2011"], "startMonth": ["May", "June" ],"endDate": ["2010","2011"],"endMonth": ["May","June"]}]}]';
var myData = JSON.parse(jsonString);
var $product_type = $('#product_type');
var $report_type = $('#report_type');
$.each(myData, function () {
$('<option>' + this.productType + '</option>').appendTo($product_type);
});
$.each(myData, function () {
$('<option>' + this.productType[0].reportNames + '</option>').appendTo($report_type);
});
Basically I want to create six drop downs like this:
productType - ABC, XYZ
reportNames - Report1, Report2
startDate - 2010, 2011
startMonth - May, June
endDate - 2010, 2011
endMonth - May, June
I am using jQuery to parse the JSON object. Here is the demo - http://jsfiddle.net/Lnv9d/3/