只是想知道是否有人可以帮我解决一些问题,我正在解析json数据。我将从JSON的spinet开始,我将不包括所有内容(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"
}
],
我需要做的是访问“X_bizCardServiceLinks”的每个子元素并将名称和href存储到变量中 - 我能够返回所有单个子项但不能单独定位每个子项 - 我使用以下内容代码:
$.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>");
});
});
响应是JSON但存储,任何帮助都将非常感谢提前,
答案 0 :(得分:0)
$.each(response.X_bizCardServiceLinks, function(index, elem) {
// elem.name
// elem.href
});
'"X_bizCardServiceLinks": []'
无论如何看起来像无效的JSON。它应该包含在{ }
:
'{"X_bizCardServiceLinks": []}'
答案 1 :(得分:0)
您正在使用每个函数为每个元素调用回调函数。遗憾的是,您没有指定参数,该参数可用于访问回调函数中的单个元素。
所以而不是
$ .each(response.X_bizCardServiceLinks,function(){
你会用的
$ .each(response.X_bizCardServiceLinks,function(index,el){
然后可以使用el.name
和el.href
获取子元素。
请参阅http://api.jquery.com/each/
中每个函数的jQuery API$.each(response.X_bizCardServiceLinks, function(index, el){
$.each(this, function(n, v){
$("#linkTable tr").append("<td><a href=\""+ el.href +"\">"+ el.name +"</a>");
});
});
答案 2 :(得分:0)
你太努力了
$.each(response.X_bizCardServiceLinks, function(){
var name = this.name;
var href = this.href;
console.log(name, href);
});