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を使用したことがないので、これはまったく新しい領域です:)

ありがとう。

スクリプトと html を含む完全な json もここにあります。

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

4

1 に答える 1

1

いくつかの問題があります:

  1. append()ドロップダウンに追加optionsするために使用し#populateます。push配列に要素を追加するために使用されます。
  2. 製品はProducts、jsonオブジェクトの配列内に含まれています。したがって、を繰り返す代わりに、;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 に答える