【问题标题】:Error in handling json data inside jquery ajax each function在jquery ajax每个函数中处理json数据时出错
【发布时间】:2014-04-19 06:47:26
【问题描述】:

祝程序员愉快

说明: 每当用户选择某个类别时,我的代码中都有下拉列表,我需要从 json 文件中搜索该值。我通过更改事件完成了此操作,效果很好。

$('#social_media').on('change',function(e){
        var test = this.value;
        $.ajax({
        url:"jsoncontent.json",
        dataType:"json",
        success:function(data){
            //here test variable contain 'facebook' value
            //so i gave data.test instead of data.facebook
            $.each(data.test,function(key,value){               
                    alert(value.name);
                });     
            }   
        });
    });

问题:这是我给 data.test 而不是 'data.facebook' 的每个循环中的问题 因为 test 是包含值 'facebook' 的变量,我知道 data 是对象,而 test 是变量,我确定我做错了在处理对象和变量..这是我的 json 数据

{
    "StackOverflow": [
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ],
    "facebook": [
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ],
    "twitter": [
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ]
} 

提前致谢

【问题讨论】:

标签: jquery ajax json each


【解决方案1】:

试试这个:

$.each(data[test], function (key, value) {
    alert(value.name);
});
}

如果test 是一个变量,那么我们使用data[test] 而不是data.test

【讨论】:

    【解决方案2】:

    您需要在第一个 $.each 内创建另一个 $.each 所以第二个$.each应该取'value'的值

    $.each(obj, (key, value) {
               $.each(obj, (keys, values) {}
               }
    

    在我看来是这样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多