2017-05-15 6 views
-2

私はJSON形式のデータをフェッチしているのpythonで関数を持っていると私はそれを私が取得しようとしています別の関数PythonのJSON出力から任意の項目の値を取得する方法は?

import requests 
import json 
import sys 
def printResponse(r): 
    print '{} {}\n'.format(json.dumps(r.json(), 
    sort_keys=True, 
    indent=4, 
    separators=(',', ': ')), r) 
r = requests.get('https://wiki.honey.com/rest/api/content', 
    params={'title' : 'Release Notes for 1.1u1'}, 
    auth=('ABCDE', '*******')) 
printResponse(r) 
getPageid = json.loads(r) 
value = int(getPageid['results']['id']) 

を使用することができますので、私は1つの項目の値を取得し、変数に格納する必要があります変数「値」のid(160925)項目の値はので、私はそれを別の関数の下

それはJSONレスポンスで "results"キーのように見える
{ 
    "_links": { 
     "base": "https://wiki.honey.com", 
     "context": "", 
     "self": "https://wiki.honey.com/rest/api/content?title=Notes+for+1.1u1" 
    }, 
    "limit": 25, 
    "results": [ 
     { 
      "_expandable": { 
       "ancestors": "", 
       "body": "", 
       "children": "/rest/api/content/160925/child", 
       "container": "", 
       "descendants": "/rest/api/content/160925/descendant", 
       "history": "/rest/api/content/160925/history", 
       "metadata": "", 
       "operations": "", 
       "space": "/rest/api/space/Next", 
       "version": "" 
      }, 
      "_links": { 
       "self": "https://wiki.honey.com/rest/api/content/160925412", 
       "tinyui": "/x/5IaXCQ", 
       "webui": "/display/Next/Notes+for+1.1u1" 
      }, 
      "extensions": { 
       "position": "none" 
      }, 
      "id": "160925", 
      "status": "current", 
      "title": "Notes for 1.1u1", 
      "type": "page" 
     } 
    ], 
    "size": 1, 
    "start": 0 
} <Response [200]> 

答えて

1

がリストに対応JSON出力がされて使用することができます、あなたが望むでしょうそのリストにインデックスを付けて辞書を取得する。

など。 \ Python27 \ libに\ JSON \ __ init__.py:getPageid['results'][0]['id']

+0

私は= json.loads(R) ファイル「CこのエラーgetPageidを取得しています"results"リスト内の最初のオブジェクトのためにあなたに"id"キーの文字列値を返す必要があります"、行339、ロード中 戻り値_default_decoder.decode ファイル" C:\ Python27 \ lib \ json \ decoder.py "、行364、デコード中 obj、end = self.raw_decode(s、idx = TypeError:予想される文字列またはバッファー – Sushant

+0

私は正しい方向を教えてくれてありがとう。私は今結果を得ることができますver = int(info ['results'] [0] [' id ']) \t 印刷バージョン – Sushant

関連する問題