我正在尝试从json结果中获取值,该结果是从我的javascript中的REST调用返回的。 以下是REST的JSON结果
{
"self": "http://example.com/rest/api/2/project/MTS/role/10002",
"name": "Administrators",
"id": 10002,
"description": "A project role that represents administrators in a project",
"actors": [{
"id": 10803,
"displayName": "Administrator ",
"type": "atlassian-user-role-actor",
"name": "admin",
"avatarUrl": "/secure/useravatar?size=small&avatarId=10108"
}, {
"id": 10590,
"displayName": "jira-administrators",
"type": "atlassian-group-role-actor",
"name": "jira-administrators",
"avatarUrl": "/secure/useravatar?size=small&avatarId=10123"
}]
}
从这个结果我只需要获取所有演员的名字 有人可以帮我用下面的脚本
function getName()
{
var user;
$.ajax({
url: "/rest/api/2/project/MITS/role/10002",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data;
}
});
return user;
}
以上脚本不正确请帮忙
答案 0 :(得分:2)
在成功功能中,使用此
users = []; // you will store the names here
$.each(data.actors, function(i,actor){
if(actor.type === "atlassian-user-role-actor"){
users.push(actor.name);
}
})
在用户中,您将获得演员姓名
如果需要,请将您的JSON粘贴到http://www.jsoneditoronline.org/,您将清楚地看到“数据”对象的内容以及如何访问它。
答案 1 :(得分:1)
您可以像data.actors
然后使用$.each
或for loop
$.each(data.actors, function(i, val) {
console.log('Actor name :: ' + val.name)
});