簡體   English   中英

jQuery UI自動完成-訪問JSON中的嵌套對象

[英]jQuery UI Autocomplete - accessing nested objects in JSON

我正在嘗試將jQuery UI自動完成小部件與自定義JSON提要結合使用,而該提要是從API返回的,其格式如下:

    {

    "SearchTerm": "ches",
    "HasDirectCountyHit": false,
    "DirectCountyHitId": null,
    "HasDirectLocationHit": false,
    "DirectLocationHitId": null,
    "Developments": [
        {
            "Id": "45339ae3e55a",
            "Label": "Chestnut Walk, Bilston",
            "Url": "/developments/chestnut-walk-bilston"
        },
        {
            "Id": "4835f52e053a",
            "Label": "Crown Park, Chester",
            "Url": "/developments/crown-park-chester"
        },
        {
            "Id": "757964964cc6",
            "Label": "The Birches, West Timperley",
            "Url": "/developments/the-birches-west-timperley"
        }
    ],
    "Counties": [
        {
            "Id": "7",
            "Label": "Cheshire",
            "Url": "/search?cid=7"
        },
        {
            "Id": "24",
            "Label": "Greater Manchester",
            "Url": "/search?cid=24"
        }
    ],
    "Locations": [
        {
            "Id": "12061",
            "Label": "Cheselbourne, Dorset (DT2 7)",
            "Url": "/search?lid=12061"
        },
        {
            "Id": "12062",
            "Label": "Chesham, Buckinghamshire (HP5 1)",
            "Url": "/search?lid=12062"
        },
        {
            "Id": "12063",
            "Label": "Chesham, Greater Manchester (BL9 6)",
            "Url": "/search?lid=12063"
        },
        {
            "Id": "12064",
            "Label": "Chesham Bois, Buckinghamshire (HP6 5)",
            "Url": "/search?lid=12064"
        },
        {
            "Id": "12065",
            "Label": "Cheshunt, Hertfordshire (EN8 9)",
            "Url": "/search?lid=12065"
        },
        {
            "Id": "12066",
            "Label": "Chesley, Kent (ME9 7)",
            "Url": "/search?lid=12066"
        },
        {
            "Id": "12067",
            "Label": "Cheslyn Hay, Staffordshire (WS6 7)",
            "Url": "/search?lid=12067"
        },
        {
            "Id": "12068",
            "Label": "Chessetts Wood, Warwickshire (B94 6)",
            "Url": "/search?lid=12068"
        },
        {
            "Id": "12069",
            "Label": "Chessington, Kingston upon Thames - Greater London (KT9 2)",
            "Url": "/search?lid=12069"
        },
        {
            "Id": "12070",
            "Label": "Chessmount, Buckinghamshire (HP5 1)",
            "Url": "/search?lid=12070"
        }
    ]

}

我正在調用的API根據搜索條件返回結果,所以我知道嵌套對象中的所有結果都是匹配的-我的問題是如何訪問這些對象(“開發”,“縣”和“位置”)以便自動完成小部件可以選擇“標簽”值?

謝謝羅賓

好的-這是您可以做的:

//put all the keys you want to pull out of your json in an array
var props = [
    "Locations", "Counties", "Developments"
];
//empty array for your autocomplete
var labels = [];

//loop thru all the properties you care about
$.each(props, function () {
    $.each(source[this], function () {
        //and pull out all the labels and add them to the labels array
        labels.push(this.Label)
    });
});


$("#autocomplete").autocomplete({
    source: labels
});

並查看其運行情況,我創建了一個快速提琴http://jsfiddle.net/fr5yb3n0/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM