开发者

Confusion over parsing looping json with jQuery

Just wondering if some one can help me with some a issue I am having parsing json data. I will start with a spinet of the JSON i won't include all of it (JSON is also valid) :

"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开发者_如何学Go@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"
    }
],

What I need to do is access each child element of the "X_bizCardServiceLinks" and store the name and href to a variable - I am able to return all of the individual children but not target each child separately - I am using the following code :

$.each(response.X_bizCardServiceLinks, function(){
            $.each(this, function(n, v){
                var random = n;
                var anotherRandom = v;
                console.log(v); 
                //$("#linkTable tr").append("<td><a href=\""+ this.href +"\">"+ this.name +"</a>");
            });

        });

Response is the JSON but stored, any help would be greatly appreciated thanks in advance,


You are trying much too hard

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

http://jsfiddle.net/F8XPM/


You’re using a callback function to be called for each element with the each function. Unfortunately you did not specify a parameter, which can be used to access the individual element in the callback function.

So instead of $.each(response.X_bizCardServiceLinks, function(){ you would use $.each(response.X_bizCardServiceLinks, function(index, el){ and could then get the child elements with el.name and el.href.

See the jQuery API for the each function at http://api.jquery.com/each/

$.each(response.X_bizCardServiceLinks, function(index, el){
    $.each(this, function(n, v){
        $("#linkTable tr").append("<td><a href=\""+ el.href +"\">"+ el.name +"</a>");
    });
});


$.each(response.X_bizCardServiceLinks, function(index, elem) {
    // elem.name
    // elem.href
});

'"X_bizCardServiceLinks": []'

Looks like invalid JSON anyways. It should be enclosed in { }:

'{"X_bizCardServiceLinks": []}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜