2016-03-24 43 views
0

我想處理從Python中的BitBucket API返回的響應對象。我有以下代碼來嘗試處理響應:在Python字典中處理來自REST API的多維響應對象

import requests 
import json 

url = 'http:www.sampleurl.com' 
myResponse = requests.get(url,auth=("myusername","mypassword")) 

jd = myResponse.json() 
print(jd.keys()) 

而且按鍵可以完美地恢復。但是,在一個特定的鍵中,返回一個列表。我對該列表中包含的值感興趣。具體而言,我關心的是「價值觀」中的價值觀。有沒有辦法在成爲列表之前/之後解析出值中包含的信息?

我的問題歸結爲我在以下示例響應中存在訪問JSON數組「父母」內部信息的問題。

{ 
    "pagelen": 30, 
    "values": [ 
    { 
     hash: "61d9e64348f9da407e62f64726337fd3bb24b466", 
     links: { 
      self: { 
       href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466" 
      }, 
      comments: { 
       href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466/comments" 
      }, 
      patch: { 
       href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/patch/61d9e64348f9da407e62f64726337fd3bb24b466" 
      }, 
      html: { 
       href: "https://api.bitbucket.org/atlassian/atlassian-rest/commits/61d9e64348f9da407e62f64726337fd3bb24b466" 
      }, 
      diff: { 
       href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/diff/61d9e64348f9da407e62f64726337fd3bb24b466" 
      }, 
      approve: { 
       href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/61d9e64348f9da407e62f64726337fd3bb24b466/approve" 
      } 
     }, 
     repository: { 
      links: { 
       self: { 
        href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest" 
       }, 
       avatar: { 
        href: "https://d3oaxc4q5k2d6q.cloudfront.net/m/bf1e763db20f/img/language-avatars/java_16.png" 
       } 
      }, 
      full_name: "atlassian/atlassian-rest", 
      name: "atlassian-rest" 
     }, 
     author: { 
      raw: "Joseph Walton <[email protected]>", 
      user: { 
       username: "jwalton", 
       display_name: "Joseph Walton", 
       links: { 
        self: { 
         href: "https://api.bitbucket.org/2.0/users/jwalton" 
        }, 
        avatar: { 
         href: "https://secure.gravatar.com/avatar/8e6e91101e3ed8a332dbebfdf59a3cef?d=https%3A%2F%2Fd3oaxc4q5k2d6q.cloudfront.net%2Fm%2Fbf1e763db20f%2Fimg%2Fdefault_avatar%2F32%2Fuser_blue.png&s=32" 
        } 
       } 
      } 
     }, 
     parents: [{ 
      hash: "59721f593b020123a75424285845325126f56e2e", 
      links: { 
       self: { 
        href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/59721f593b020123a75424285845325126f56e2e" 
       } 
      } 
     }, { 
      hash: "56c49d8b2ae3a094fa7ba5a1251d6dd2c7c66993", 
      links: { 
       self: { 
        href: "https://api.bitbucket.org/2.0/repositories/atlassian/atlassian-rest/commit/56c49d8b2ae3a094fa7ba5a1251d6dd2c7c66993" 
       } 
      } 
     }], 
     date: "2013-10-21T07:21:51+00:00", 
     message: "Merge remote-tracking branch 'origin/rest-2.8.x' " 
    } 
    ], 
    "page": 1 
} 
+0

你只是想減少響應數據只有你感興趣的鑰匙?我是否誤解了這個問題? –

+0

這是我正在尋找的副作用。我正在尋找扁平化響應對象,以便我可以將作者姓名,消息,時間全部在一個層次上。 我的根本問題是,對於單個提交,我無法訪問諸如作者姓名之類的信息,因爲它將多個圖層埋入json響應中。 –

+0

1)沒有一種編程方式可以做到這一點,因爲嵌套的Dicts可以複製鍵名。 2)您使用什麼語言來查看/解析JSON響應,因爲無論深度如何,Python都可以輕鬆地獲取您正在查找的數據。我想我無法從你的代碼中知道哪部分不能工作,以及你需要的是什麼,而不是你所得到的。 –

回答

1

不知道我是否正確理解了這個問題,但是您的字典缺少jDo提到的引號。假設這是一個很好形成快譯通,你應該能夠訪問像這樣的家長信息:

parents = jd["values"][0]["parents"] 

然後你可以在家長列表循環,做任何你用它想:

for parent in parents: 
    do_sth_with_parent(parent["hash"], parent["links"]) 

這是否回答了這個問題?

+0

僅供參考,該字典不缺少引號,bitbucket在編寫文檔時很懶惰。我一直在解決這個問題,並接近看起來像你的幫助,所以我相信你是正確的。我的問題是訪問數組中的元素,我認爲你已經釘住了它。 –