循环遍历Web服务对象并创建下拉列表

时间:2015-05-25 11:02:30

标签: jquery html ajax json

我需要使用来自Web服务的数据创建一个下拉列表,它提供了以下格式的JSON:

{
    "ErrorInfo": {
        "Success": true,
        "ErrorCode": "",
        "Program": "",
        "Method": "",
        "Message": "",
        "Details": "",
        "StackTrace": "",
        "ErrorList": null
    },
    "Results": {
        "DimName": "region",
        "SubsetName": "",
        "Members": [{
            "ID": "Central Europe",
            "Name": "Central Europe",
            "Children": [],
            "Hierarchy": [],
            "Attributes": []
        },
        {
            "ID": "Southern Europe",
            "Name": "Southern Europe",
            "Children": null,
            "Hierarchy": [],
            "Attributes": []
        }]
    }
}

我的HTML代码是:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <style>
  img {
    height: 100px;
    float: left;
  }
  </style>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div id="images"></div>

<script>
$.ajax({
    url: "http://localhost/somewebservice/json/Europe?",
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json',
    processData: true,
    success: function (data,value) {
      var obj = JSON.stringify(data,value);
    },
    error: function(){
      alert("Cannot get data");
    }
});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

考虑到要将Members填充到下拉列表中,可以使用jquery的each函数循环遍历数组。

each函数将数组作为参数。

$.each(data.Results.Members,function(index,value){ // here value.Name or value. })