0

我有一个包含大量信息的 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 和 Description 并用它们填充选择/选项列表。

<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>');
  });
        });
    });
});​

我也尝试了不同版本的代码,但无济于事。我错过了什么重要的东西吗?可能是json格式错误吗?

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

谢谢。

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

http://jsfiddle.net/Kyle_Sevenoaks/dkZTb/

4

1 回答 1

1

有几个问题:

  1. 用于append()添加options#populate下拉列表。push用于向数组添加元素;
  2. 您的产品包含在Productsjson 对象的数组中。因此,不是迭代,而是data迭代data.ListProductsResponse.Products
  3. 有一个小的语法错误,因为您没有">.each().

这是一个固定的例子:

var $select = $("#populate");
$.each(data.ListProductsResponse.Products, function(entryIndex, entry) {
  $select.append('<option value="' + entry.ProductID + '">' + entry.Description + '</option>');
});

演示

于 2012-09-18T09:38:20.907 回答