如何从POST响应中迭代保存JSON页面?

时间:2015-07-03 18:28:20

标签: python json python-requests

我正在使用JSON网络服务,我正在批量接收多条记录,一次最多只能接收100条记录。我的响应中有参数显示每个请求发送的记录是否超过100条。如果有,我的逻辑再次调用该服务并动态更改请求。我的问题是,如何保存EACH响应,而不是最后一个响应。

这是我的剧本:

f2 =open('C:\Users\GeoffreyWest\Desktop\ContainerRequest.json')
data2 = jsonpickle.decode((f2.read()))
#do stuff for data2
data2 = jsonpickle.encode(data2)
lastpage = 'false'
startrow = 0
newquery = 'new'
pagesize = 100

url2 = "URL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}

while lastpage == 'false':
    r2 = requests.post(url2, data=data2, headers=headers2)
    print r2.text
    decoded2 = json.loads(r2.text)
    f2 =open('C:\Users\GeoffreyWest\Desktop\ContainerRequest.json')
    data2 = jsonpickle.decode((f2.read()))
    if decoded2['Response']['LastPage'] == 'false':
        data2['QueryRequest']['PageSize'] = pagesize
        startrow = startrow + data2['QueryRequest']['PageSize']
        data2['QueryRequest']['StartRowNum'] = startrow
        data2['QueryRequest']['NewQuery'] = 'false'
        data2 = jsonpickle.encode(data2)
        print startrow
    else:
        lastpage = 'true'

样品申请:

{
    "MetaData": {},
    "QueryRequest": {
        "LastUpdatedDate": "06/29/2015 13:00:00",
        "srType": [
            "Bulky Items",
            "Electronic Waste",
            "Metal/Household Appliances",
            "Dead Animal Removal",
            "Manual Pickup",
            "Brush Items Pickup",
            "Move In/Move Out",
            "Illegal Dumping Pickup"

        ],
        "NewQuery": "new",
        "PageSize": "100",
        "StartRowNum": "0"
    }
}

示例响应需要循环才能继续分页:

"Response": {
        "LastPage": "false",
        "NumOutputObjects": "100",
        "ListOfServiceRequest": {

0 个答案:

没有答案