简体   繁体   English

如何使用JavaScript在for循环中获取响应json数据

[英]How to get Response json data in for loop using javascript

Below listed is my json response. 下面列出的是我的json响应。 My purpose is to get this data in javascript. 我的目的是在javascript中获取此数据。 My firebug console shows below result. 我的Firebug控制台显示以下结果。 Please help me to get data in js. 请帮助我获取js中的数据。

{"ProfessionFile":[[{"id":34,"title_en":"CEO"}],[{"id":35,"title_en":"PM"}]]}

Alert shows "undefined" result JS 警报显示“未定义”结果JS

$(".tag-handler-ambition").ready(function () {
    $.get("/profession/file", {tagcategoryid: 3}, function (response) {
            for (var i = 0; i < response.ProfessionFile.length; i++) {
                alert(response.ProfessionFile[i].id);
                $(".tag-handler-ambition").after('<div id="filename-response_' + response.ProfessionFile[i].id + '"><a href="/profession/download/' + response.ProfessionFile[i].id + '">' + response.ProfessionFile[i].title_en + '.pdf</a></div>');
            }
    });
});

You should parse this response as a JSON, othewise JavaScript will not see it as an object: 您应该将此响应解析为JSON,否则JavaScript不会将其视为对象:

$(".tag-handler-ambition").ready(function () {
    $.get("/profession/file", {tagcategoryid: 3}, function (response) {
        response = $.parseJSON(response);
        for (var i = 0; i < response.ProfessionFile.length; i++) {
            $(".tag-handler-ambition").after('<div id="filename-response_' + response.ProfessionFile[i].id + '"><a href="/profession/download/' + response.ProfessionFile[i].id + '">' + response.ProfessionFile[i].title_en + '.pdf</a></div>');
        }
    });
});

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

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