0

我需要在我的 java 脚本中从这个 JSON 中获取值,这来自 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"
            }
        ]
    }
]

有什么建议么?我想像这样获取它:

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);

// 警报(json.subSelections);循环内部给了我未定义的值。

4

1 回答 1

0

尝试这个:

$.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);
于 2013-04-22T18:44:01.157 回答