【问题标题】:Iterating through Json in Jquery在 Jquery 中遍历 Json
【发布时间】:2011-10-06 00:48:51
【问题描述】:

大家好,提前感谢您帮助我。首先,如果我在这里使用了错误的短语,我深表歉意。我并不担心这里的语法,只是让它工作。现在,问题是:我有 django 序列化程序输出以下 JSON:

[
    {
        "pk": 11262, 
        "model": "dict.words", 
        "fields": {
            "lang": "KO", 
            "incorrect": null, 
            "sug_translation": [
                3215
            ], 
            "word": "\uc0dd\uac01\ud558\ub2e4", 
            "definition": [], 
            "pos": "VE", 
            "phrase": [], 
            "translation": [
                {
                    "pk": 1, 
                    "model": "dict.words", 
                    "fields": {
                        "word": "comprender"
                    }
                }, 
                {
                    "pk": 6028, 
                    "model": "dict.words", 
                    "fields": {
                        "word": "entender"
                    }
                }
            ], 
            "incomplete": null
        }
    }
]

我想做的是访问 fields.translation.fields.words,因此自动完成的 Jquery 是

$(function() {

             $( "#query_form" ).autocomplete({
                     minLength: 2,
                     source: 'http://127.0.0.1:8000/json_results/',
                     focus: function( event, ui ) {
                             $( "#query_form" ).val( ui.item.word );
                             return false;
                     },
             select: function( event, ui ) {
                     $.get ('http://127.0.0.1:8000/json_detail/',
                                {
                                 item: ui.item.pk
                                },
                                function(data) {
                                 $('#query_result').prepend(data);
                                });
                     $( "#query_form" ).val( ui.item.word );
                     return false;
             }
     })
     .data( "autocomplete" )._renderItem = function( ul, item ) {
                        var tran = $.each(item.fields.translation, function(i){item.fields.translation[i].fields.word})
                return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append("<a>" + item.fields.word + tran + "</a>")
                        .appendTo( ul );
     };
});

总的来说,我对 jquery 和 javascript 非常陌生,所以请原谅任何明显的格式错误。无论如何,这里的问题是,虽然这确实发出了请求和自动完成功能,但 $.each(item.fields.translation, function(i) item.fields.translation[i].fields.word}) 返回[object, Object] 在自动完成列表中。如果我通过 alert() 输出它,它会返回正确的值。如果我只在 .append 行中使用 item.fields.translation[0].fields.word,它会输出值。但是由于某种原因,当我要求它做我想做的事情时,我得到 [object Object] 那么,有人知道我做错了什么吗?提前致谢!

【问题讨论】:

    标签: jquery json autocomplete


    【解决方案1】:

    您将变量tran 设置为等于$.each() 函数,即returns jQuery。这就是它最终成为对象的原因。

    我不清楚你到底想做什么。您正在循环遍历 item.fields.translation 数组中的项目,但最终会执行附加操作,就好像该循环应该只返回一个字符串一样。但是item.fields.translation 的数组中有两个项目,所以... 你可以像这样构建一个数组:

    var tran = [];
    $.each(item.fields.translation, function(i){
        tran.push(item.fields.translation[i].fields.word);
    });
    //tran now equals ['comprender','entender']
    

    不确定这是否有帮助。如果您澄清您对 tran 的期望,那么我可以提供进一步的帮助。

    附带说明:您可以将当前迭代中项目的值(而不仅仅是其索引/键)传递给$.each() 中的函数。例如:

    $.each(item.fields.translation, function(i,v){
        //the following is the same as tran.push(item.fields.translation[i].fields.word);
        tran.push(v.fields.word);
    });
    

    【讨论】:

    • 第一个例子正是我想要的。我对推送一无所知(就像我说的,完全是 js 的菜鸟)所以你,我的朋友,是我的新英雄。谢谢谢谢谢谢!立即工作:D
    猜你喜欢
    • 2017-06-23
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多