2013-06-21 59 views
-2

我想從json結果中獲取值,這是從我的javascript中的REST調用返回的結果。 下面的REST如何從JSON結果返回從REST調用返回的具體值

{ 
    "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" 
    }] 
} 

從這個結果,我只需要獲取JSON結果所有的演員的名字 有人可以幫我用下面的腳本

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

的輸出是什麼'的console.log(用戶)'?試着把它放在瀏覽器控制檯中看看是否有東西。 – Jai

回答

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/,你會清楚地看到什麼「數據」對象有以及如何訪問它。

+0

謝謝安東尼。這有助於 – Mizan

+0

嗨安東尼斯,還有一件事。我只需要獲得那些type =「atlassian-user-role-actor」的演員。請你幫忙 – Mizan

+0

我編輯它,這很簡單,我想。 – AntouanK

1

您可以通過訪問它像data.actors

然後使用數組在迭代得到演員陣列$.eachfor loop

$.each(data.actors, function(i, val) { 
    console.log('Actor name :: ' + val.name) 
});