0

I need to fetch the values from this JSON in my java script this is coming from jsp:

[{
        "selectionName": "Select",
        "subSelections": [{
                "id": 4,
                "subSelectionName": "Select",
                "description": "Deepmala"
            }
        ]
    }, {
        "selectionName": "week14",
        "subSelections": [{
                "id": 7,
                "subSelectionName": "1",
                "description": ""
            }
        ]
    }, {
        "selectionName": "test",
        "subSelections": [{
                "id": 6,
                "subSelectionName": "test",
                "description": ""
            }
        ]
    }, {
        "selectionName": "select",
        "subSelections": [{
                "id": 3,
                "subSelectionName": "sub-select",
                "description": "Created by Prakash"
            }
        ]
    }, {
        "selectionName": "testcreate",
        "subSelections": [{
                "id": 1,
                "subSelectionName": "testcreate",
                "description": ""
            }
        ]
    }, {
        "selectionName": "by htmlwidget",
        "subSelections": [{
                "id": 5,
                "subSelectionName": "by htmlwidget",
                "description": "created by html widget"
            }
        ]
    }
]

Any suggestions? I am tring to fetch it like this:

function getSelection() {
  var options = "";  
  $.getJSON('../r3/selection.jsp').done(function(json) {    

  //alert(json.selectionName);
  // alert(json.subSelections);
  // options += '<option value="' + value. selectionId + '">' + value.selectionName +   '</option>';
    $.each(json.subSelections, function(index, value) {

     options += '<option value="' + value. subSelectionName + '">' + value. description + '</option>';
});

     var select = $('<select id="selection" onchange="getSubselection()"/>');   
    select.append(options);
    $(document.body).append(select); 

   }).fail(function (jqxhr, textStatus, error) {
   alert(' fail json  : '+error);
  }); 

}
//alert(json.selectionName);

// alert(json.subSelections); inside the loop gives me undefined value.

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
Deepmala
  • 1
  • 1

1 Answers1

0

Try this:

$.each(json, function (key, data) {
    $.each(data.subSelections, function (index, values) {
        options += '<option value="' + values.subSelectionName + '">' + values.description + '</option>';            
    });
});

var select = $('<select id="selection" onchange="getSubselection()"/>');
select.append(options);
$('body').append(select);
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Hi Palash,I need todisplay theses values on combo/drop box .First combobox will contain the selectionName,while the second combobox will contain the subselection name based on the selection name.I am trying this but it fetches all the values in the subslection for .posting the code in following comment. – Deepmala Apr 23 '13 at 15:01
  • var options,suboptions = ""; var select = $('');var subselect = $('');$.getJSON('/.r3/selection.jsp').done(function(json) { $.each(json, function (key, data) {options += ''; $.each(data.subSelections, function (index, values) { suboptions += '';}); }); select.append(options); subselect.append(suboptions); ...... – Deepmala Apr 23 '13 at 15:08
  • ..$(document.body).append(select);$(document.body).append(subselect); })} – Deepmala Apr 23 '13 at 15:08
  • JSON coming out of the jsp(selection.jsp) is the one posted at the very begining of the question :http://jsfiddle.net/LUpq3/1/ – Deepmala Apr 23 '13 at 16:08
  • http://jsfiddle.net/LUpq3/1/ the json coming out of selection.jsp is the same as posted at the bebining of the question.@Palash Mondal – Deepmala Apr 23 '13 at 16:15