2013-12-21 123 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