简体   繁体   English

迭代JSON对象以获取自动完成建议

[英]Iterating over JSON object for autocomplete suggestions

I receive the following object that I receive from the source - 我从源头收到以下对象 -

["road",[["road",53,"0E"],["roadtrip",53,"1E"],["roadrunner",53,"2E"],["roadster",53,"3E"],["roadside",53,"4E"],["roadrage",53,"5E"],["roadcycling",53,"6E"],["roadsideamerica",53,"7E"]],{"k":1,"q":"lpxvQlflqTsJ-2_PMBLImSpu1PU"}]

Values I want to fetch from the object: 我想从对象中获取的值:

road, roadtrip, roadrunner, roadster and so on.

and here's the code 这是代码

    $('#query').autocomplete({
        source: function( request, response ) {
        $.ajax({
        type: "GET",
        url: , // url for the source
        dataType: "json",
        data: { 
            "tags" : request.term
        },
        success: function( data ) {
            related = data[1];
            response(data[1][0]);
        }
        });
    }

But I get only the value from the first array of the object? 但我只得到对象的第一个数组的值? For eg. 例如。 road OE

How should iterate to fetch the values of it? 应该如何迭代来获取它的值?

That's an array not an object and if you JSONLint it: 这是一个数组而不是一个对象,如果你JSONLint它:

[
    "road",
    [
        [
            "road",
            53,
            "0E"
        ],
        [
            "roadtrip",
            53,
            "1E"
        ],
        [
            "roadrunner",
            53,
            "2E"
        ],
        [
            "roadster",
            53,
            "3E"
        ],
        [
            "roadside",
            53,
            "4E"
        ],
        [
            "roadrage",
            53,
            "5E"
        ],
        [
            "roadcycling",
            53,
            "6E"
        ],
        [
            "roadsideamerica",
            53,
            "7E"
        ]
    ],
    {
        "k": 1,
        "q": "lpxvQlflqTsJ-2_PMBLImSpu1PU"
    }
]

You can see that the first key is the string "road", second is an array and the last an object. 您可以看到第一个键是字符串“road”,第二个键是数组,最后一个是对象。

The items you are looking for are the first keys of the arrays in the second key of the main array. 您要查找的项目是主阵列第二个键中数组的第一个键。

Try something like: 尝试类似的东西:

$('#query').autocomplete({
    source: function( request, response ) {
    $.ajax({
    type: "GET",
        url: , // url for the source
        dataType: "json",
        data: { 
            "tags" : request.term
        },
        success: function( data ) {
            var terms = [];

            for (var i=0; i < data[1].length; i++) {
                terms.push(data[1][i][0]);
            }

            respond(terms);
        }
    });
}

This should give you all the elements you're looking for. 这应该为您提供您正在寻找的所有元素。 How you then iterate them is up to you: 你如何迭代它们取决于你:

jQuery.map(data[1], function(item) {return item[0];});
// output: ["road", "roadtrip", "roadrunner", "roadster", ... ]

BTW, to those saying "It's not an Object, it's an Array," an Array is a type of Object in JS. 顺便说一句,对于那些说“它不是一个对象,它是一个数组”的人来说,一个数组是JS中的一种对象。

-- Scott - 斯科特

The data provided is not an object. 提供的数据不是对象。 You have an array, which is indicated by the use of brackets '[..]'. 你有一个数组,用括号'[..]'表示。 In addition, objects have properties and values, ie { "road": 53 } . 此外,对象具有属性和值,即{ "road": 53 }

Assuming you had a properly formed object, then you can utilize the $.getJSON() function in jQuery. 假设你有一个正确形成的对象,那么你可以在jQuery中使用$ .getJSON()函数。

You could then traverse the valid object and access the desired properties accordingly. 然后,您可以遍历有效对象并相应地访问所需的属性。

HTML : HTML

<div></div>

jQuery: jQuery的:

var obj = {     
    "road": [
        {
            "name": "roadtrip"
        },
        {
            "name": "roadrunner"
        }
    ]
};

var data = obj.road;

for(var i=0; i < data.length; i++){
    $('div').append(data[i].name + '<br>');
}

Demo: http://jsfiddle.net/6S6DJ/ 演示: http//jsfiddle.net/6S6DJ/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM