python + json:解析列表

时间:2013-12-21 21:13:49

标签: python json list parsing

我对使用python(使用python 2.7)解析JSON数据有些新意。有一项服务我必须发送API调用,而JSON响应就像我在下面所做的那样。 “行”中的项目数量可能会有所不同。我需要做的是只从第二行获取“内容”,如果有第二行,则将其放入列表中。从本质上讲,它只是“广告系列确认号码”的列表,而不是其他任何内容。如果有帮助的话,这个数字也总是只有9个数字。任何建议都将非常感谢。

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

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:-1)

假设'response'包含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']