2013-12-21 169 views
2

我有點新的(使用Python 2.7)和Python解析JSON數據。有一個服務,我必須發送API調用,並且JSON響應如下所示。 '行'中的項目數量可能會有所不同。我需要做的是隻取第二行的「內容」,如果有第二行,並將其放入列表中。本質上,它只是一個「活動確認號碼」的列表,沒有別的。如果有幫助的話,這個數字總是隻有9個數字。任何建議將非常感激。蟒蛇+ JSON:解析列出

{"response": 
    {"result": 
     {"Potentials": 
      {"row": 
       [ 
       {"no":"1","FL": 
        {"content":"523836000004148171","val":"POTENTIALID"} 
       }, 
       {"no":"2","FL": 
        {"content":"523836000004924051","val":"POTENTIALID"} 
       }, 
       {"no":"3","FL": 
        [ 
        {"content":"523836000005318448","val":"POTENTIALID"}, 
        {"content":"694275295","val":"Campaign Confirmation Number"} 
        ] 
       }, 
       {"no":"4","FL": 
        [ 
        {"content":"523836000005318662","val":"POTENTIALID"}, 
        {"content":"729545274","val":"Campaign Confirmation Number"} 
        ] 
       }, 
       {"no":"5","FL": 
        [ 
        {"content":"523836000005318663","val":"POTENTIALID"}, 
        {"content":"903187021","val":"Campaign Confirmation Number"} 
        ] 
       }, 
       {"no":"6","FL": 
        {"content":"523836000005322387","val":"POTENTIALID"} 
       }, 
       {"no":"7","FL": 
        [ 
        {"content":"523836000005332558","val":"POTENTIALID"}, 
        {"content":"729416761","val":"Campaign Confirmation Number"} 
        ] 
       } 
       ] 
      } 
     }, 
    "uri":"/crm/private/json/Potentials/getSearchRecords"} 
} 

編輯:輸出用於該實施例的一個例子將是:

confs = [694275295,729545274,903187021,729416761]

confs = [ '694275295' ,「729545274」,「903187021」,「729416761」]

真的,如果他們存儲爲字符串或整數

沒關係

編輯2:這裏是我的代碼剪斷:

import urllib 
import urllib2 
import datetime 
import json 

key = '[removed]' 

params = { 
    '[removed]' 
    } 

final_URL = 'https://[removed]' 
data = urllib.urlencode(params) 
request = urllib2.Request(final_URL,data) 
response = urllib2.urlopen(request) 
content = response.read() 

j = json.load(content) 

confs = [] 
for no in j["response"]["result"]["Potentials"]["row"]: 
    data = no["FL"] 
    if isinstance(data, list) and len(data) > 1: 
     confs.append(int(data[1]["content"])) 

print confs 
+0

您可以根據此示例添加所需輸出示例嗎? – martincho

+0

剛剛添加的例子 – crookedleaf

回答

5

假設j是上面的結構已經被解析到您的JSON對象:

>>> results = [] 
>>> for no in j["response"]["result"]["Potentials"]["row"]: 
...  data = no["FL"] 
...  if isinstance(data, list) and len(data) > 1: 
...   results.append(int(data[1]["content"])) 
... 
>>> results 
[694275295, 729545274, 903187021, 729416761] 
+0

看起來像我得到一個錯誤:AttributeError的:「海峽」對象有沒有屬性「讀」。我已將我的代碼片段添加到我的帖子中。 – crookedleaf

+0

@ user3126085:首先,您需要解析保存(從文件)使用'json.load()'的JSON信息()'字符串或'json.loads(從字符串)。 –

+0

我做到了。編輯我的帖子以顯示我的代碼。 – crookedleaf

-1

假設 '迴應' 持有JSON字符串:

import json 

data = json.loads(response) 
rows = data['response']['result']['Potentials']['rows'] 
output = [] 
for row in rows: 
    contents = row['FL'] 
    if len(contents) > 1: 
     output.append(contents[1]['content']) 

應該這樣做。

編輯:

我終於有一些時間來測試這個「單線」。使用Python的功能特性很有趣:

import json 

#initialize response to your string 
data = json.loads(response) 
rows = data['response']['result']['Potentials']['row'] 
output = [x['FL'][1]['content'] for x in rows if isinstance(x['FL'], list) and len(x['FL']) > 1] 
print output 

['694275295', '729545274', '903187021', '729416761'] 
+0

這將不起作用,因爲如果只有一個元素,它不會被包含在列表中,所以'len()'也會返回'2',因爲字典中有兩個元素。 –

+0

獲取錯誤:KeyError:'rows' – crookedleaf