使用$ .each循环JSON响应

时间:2011-02-02 12:05:03

标签: jquery json

你有一个问题我只能想到以一种非常粗暴的方式解决 - 想知道是否有其他人有任何其他想法 - 基本上我解析一些JSON并将每个孩子附加到一个div,但是一旦我附加了4个项目然后我需要将剩余的项添加到另一个div,这里是我使用它的JSON是有效的,这只是JSON的一小部分:

    "X_bizCardServiceLinks": [
    {
        "name": "blogs",
        "js_eval": "generalrs.label_personcard_blogslink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/blogs\/roller-ui\/blog\/dbb8fac0-42e4-102e-9409-b38b9530f95e"
    },
    {
        "name": "quickr",
        "js_eval": "generalrs.label_personcard_quickrlink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/quickr\/allfiles\/people\/Jonathan.Popoola@trinitymirror.com"
    },
    {
        "name": "profiles",
        "js_eval": "generalrs.label_personcard_profilelink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/profiles\/html\/simpleSearch.do?searchFor=dbb8fac0-42e4-102e-9409-b38b9530f95e&searchBy=userid"
    },
    {
        "name": "activities",
        "js_eval": "generalrs.label_personcard_activitieslink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/activities\/service\/html\/mainpage#dashboard%2Cmyactivities%2Cuserid%3Ddbb8fac0-42e4-102e-9409-b38b9530f95e%2Cname%3DJonathan Popoola"
    },
    {
        "name": "dogear",
        "js_eval": "generalrs.label_personcard_dogearlink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/dogear\/html?userid=dbb8fac0-42e4-102e-9409-b38b9530f95e"
    },
    {
        "name": "communities",
        "js_eval": "generalrs.label_personcard_communitieslink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/communities\/service\/html\/allcommunities?userid=dbb8fac0-42e4-102e-9409-b38b9530f95e"
    },
    {
        "name": "wikis",
        "js_eval": "generalrs.label.personcard.wikislink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/wikis\/home\/search?uid=dbb8fac0-42e4-102e-9409-b38b9530f95e&name=Jonathan Popoola"
    },
    {
        "name": "files",
        "js_eval": "generalrs.label_personcard_fileslink",
        "href": "https:\/\/dc3-epag-03.tm-gnet.com\/files\/app\/person\/dbb8fac0-42e4-102e-9409-b38b9530f95e"
    }
],

我目前正在尝试以下方法:

    $.each(response.X_bizCardServiceLinks, function(){
            var name = this.name;
            var href = this.href;            

            if (name != "dogear") {

                $("#linkTable tr").append("<td><a href=\""+ href +"\">"+ name +"</a>");
            }
            else {
                    console.log(name, href);
                }
            });

正如您所看到的,一旦名称等于“dogear”,该功能将移至其他但仅返回该链接而不是剩余的,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我建议使用参数jquery提供给你的回调,它给你一个索引和一个值。

 $.each(response.X_bizCardServiceLinks, function(index, val){
        var name = this.name;
        var href = this.href;            

        if (index < 4) {

            $("#linkTable tr").append("<td><a href=\""+ href +"\">"+ name +"</a>");
        }
        else {
                console.log(name, href);
            }
        });

答案 1 :(得分:0)

我猜这是一个数学问题:
$.each(response.X_bizCardServiceLinks, function(i, val){

        var mod = i % 4;
        var name = this.name;  
        var href = this.href;            
        if (mod==0) {

            $("#linkTable tr").append("<td><a href=\""+ href +"\">"+ name +"</a>");
        }
        else {
                console.log(name, href);
            }
        });

var mod = i % 4; var name = this.name; var href = this.href; if (mod==0) { $("#linkTable tr").append("<td><a href=\""+ href +"\">"+ name +"</a>"); } else { console.log(name, href); } }); 或者我可能不理解你需要什么......但是通过这种方法,你将总是分开4个元素......