Jquery 如何获取JSON对象的某些部分并将它们作为选项添加到SELECT中?

Jquery 如何获取JSON对象的某些部分并将它们作为选项添加到SELECT中?,jquery,html,ajax,json,Jquery,Html,Ajax,Json,我有一个json文件,包含很多信息 { "ListProductsResponse": { "Products": [{ "VatAmount": 0, "VatFormat": "Percent", "Bonus": { "AdditionalValue": 0, "Description": null,

我有一个json文件,包含很多信息

{
    "ListProductsResponse": {
        "Products": [{
            "VatAmount": 0,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "50 kr",
            "ExternalProductID": "TELIA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "50 kr",
                "FixedDiscount": 0,
                "Price": 5000,
                "ValidFrom": "\/Date(1293836400000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK010TELIASEKONTANT50",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-01",
            "ValidTo": "2061-11-19",
            "Value": 5000
        }, {
            "VatAmount": 0,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "100 kr",
            "ExternalProductID": "TELIA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "100 kr",
                "FixedDiscount": 0,
                "Price": 10000,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK020TELIASEKONTANT100",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 10000
        }, {
            "VatAmount": 0,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "150 kr",
            "ExternalProductID": "TELIA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "150 kr",
                "FixedDiscount": 0,
                "Price": 15000,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK030TELIASEKONTANT150",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 15000
        }, {
            "VatAmount": 0,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "200 kr",
            "ExternalProductID": "TELIA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "200 kr",
                "FixedDiscount": 0,
                "Price": 20000,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK040TELIASEKONTANT200",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 20000
        }, {
            "VatAmount": 0,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "300 kr",
            "ExternalProductID": "TELIA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "300 kr",
                "FixedDiscount": 0,
                "Price": 30000,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK050TELIASEKONTANT300",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 30000
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Halvårskort SMS 299 kr",
            "ExternalProductID": "TELIA-6MSMS",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Halvårskort SMS 299 kr",
                "FixedDiscount": 0,
                "Price": 29900,
                "ValidFrom": "\/Date(1323298800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK060TELIASEHALVAARSMS",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-12-08",
            "ValidTo": "2061-12-09",
            "Value": 29900
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Halvårskort Mobilsurf 299 kr",
            "ExternalProductID": "TELIA-6MDATA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Halvårskort Mobilsurf 299 kr",
                "FixedDiscount": 0,
                "Price": 29900,
                "ValidFrom": "\/Date(1326927600000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK070TELIASEHALVAARMOBSURF",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2012-01-19",
            "ValidTo": "2062-01-19",
            "Value": 29900
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Årskort Mobilsurf 499 kr",
            "ExternalProductID": "TELIA-YDATA",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Årskort Mobilsurf 499 kr",
                "FixedDiscount": 0,
                "Price": 49900,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK080TELIASEKSURF1YEAR",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 49900
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Årskort SMS & Surf 899 kr",
            "ExternalProductID": "TELIA-YCOMBO",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Årskort SMS & Surf 899 kr",
                "FixedDiscount": 0,
                "Price": 89900,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK090TELIASEKCOMBO1YEAR",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 89900
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Årskort Navigator & Surf 699",
            "ExternalProductID": "TELIA-YNAV",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Årskort Navigator & Surf 699",
                "FixedDiscount": 0,
                "Price": 69900,
                "ValidFrom": "\/Date(1323298800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK100TELIASEARSKORT",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-12-08",
            "ValidTo": "2061-12-09",
            "Value": 69900
        }, {
            "VatAmount": 2500,
            "VatFormat": "Percent",
            "Bonus": {
                "AdditionalValue": 0,
                "Description": null,
                "ValidFrom": "\/Date(-1807877931046+0200)\/",
                "ValidTo": "\/Date(4503555668953+0200)\/"
            },
            "CurrencyIsoString": "SEK",
            "Description": "Årskort SMS  499 kr",
            "ExternalProductID": "TELIA-YSMS",
            "Prices": {
                "CurrencyIso": "SEK",
                "Description": "Årskort SMS  499 kr",
                "FixedDiscount": 0,
                "Price": 49900,
                "ValidFrom": "\/Date(1294786800000+0100)\/",
                "ValidTo": "\/Date(-62135596800000+0100)\/"
            },
            "ProductID": "KK110TELIASEKSMS1YEAR",
            "ProviderId": "TELIA",
            "UnitType": null,
            "ValidFrom": "2011-01-12",
            "ValidTo": "2061-01-12",
            "Value": 49900
        }]
    },
    "Header": {
        "AcquirerID": "OKB",
        "AgreementID": "92010002",
        "ClientIP": "77.40.160.226",
        "MerchantID": "Test TopupService",
        "TransmissionTime": "2012-10-10 00:00:00"
    },
    "Status": {
        "OperationStatus": "0",
        "OperationStatusDesc": "",
        "TransactionStatus": 0,
        "TransactionStatusDesc": "OK"
    }
}
我需要获得每个产品的ProductID和描述,并用它们填充一个选择/选项列表

<div>
      <button id="getButton">Get</button>
      <select id="populate">
          <option>control</option>
      </select>
</div>

得到
控制
这是简单的HTML,我尝试了以下jQuery:

$(document).ready(function() {
    //attach a jQuery live event to the button
    $('#getButton').live('click', function() {
        $.getJSON('json.json', function(data) {
            $.each(data, function(entryIndex, entry) {
                items.push('<option value="' + entry['ProductID'] + ">' + entry ['Description'] + '</option>');
  });
        });
    });
});​
$(文档).ready(函数(){
//将jQuery live事件附加到按钮
$('#getButton').live('单击',函数()){
$.getJSON('json.json',函数(数据){
$.each(数据、函数(entryIndex、条目){
items.push(“”+条目['Description']+“”);
});
});
});
});​
我也尝试过不同版本的代码,但都没有用。我错过了什么重要的东西吗?json格式可能有误吗

值得注意的是,我以前从未使用过任何json/ajax,因此这是一个全新的领域:)

谢谢

完整的json以及脚本和html也可以在这里找到


有几个问题:

  • 用于将
    选项
    添加到
    #填充
    下拉列表中
    push
    用于向数组中添加元素
  • 您的产品包含在json对象的
    products
    数组中。因此,不要迭代
    数据
    ,而是迭代
    数据.ListProductsResponse.Products
  • 有一个小语法错误,因为您没有关闭
    .each()
    中的
    “>
    字符串
  • 下面是一个固定的示例:

    var $select = $("#populate");
    $.each(data.ListProductsResponse.Products, function(entryIndex, entry) {
      $select.append('<option value="' + entry.ProductID + '">' + entry.Description + '</option>');
    });
    
    var$select=$(“#填充”);
    $.each(data.ListProductsResponse.Products,函数(entryIndex,条目){
    $select.append(“”+entry.Description+“”);
    });
    

    你能发布
    json.json的完整内容吗?它是一个数组吗?添加了到json的链接。:)它确实是一个数组。哦,我明白了,那么你需要迭代这个数组而不是整个json对象。请看我的答案。是的,这就是问题所在。非常感谢你的帮助:)