处理请求响应

Handling requests response

我正在连接到 REST 服务,我收到了该格式的响应

{
"Outputs": {
    "Actual": [
        "2017-08-29T14:37:47.137",
        "2017-08-30T13:07:09.563",
        "2017-08-30T14:41:29.023"
    ],
    "Start": [
        "2017-08-29T14:36:12.42",
        "2017-08-30T12:59:53.05",
        "2017-08-30T14:40:45.34"
    ],
    "NumScrapsList": [
        0,
        3,
        ...
        

但我想要那种形式

{
"Outputs":[
    {   
        "NumScrapsList":0,
        "Actual":"2017-08-29T14:37:47.137",
        "Start":"08-29T14:36:12.42"
    },
    {
        "NumScrapsList":3,
        "Actual":"2017-08-30T13:07:09.563",
        "Start":"2017-08-30T12:59:53.05"
    }
]

}

我对 Python 代码和 JSON 格式很陌生,我不知道从哪里开始“重新映射”结果。你能告诉我正确的方向吗?

这是我的一小段代码:

with requests.post(url, headers=headers, data=data, stream=True) as r:
r.raise_for_status()
with open('outfile.json', 'wb') as f_out:
    for chunk in r.iter_content(chunk_size=8192): 
        f_out.write(chunk)

谢谢

这是适用于您的用例的通用方法:

from pprint import pprint

resp = {
    "Outputs": {
        "Actual": [
            "2017-08-29T14:37:47.137",
            "2017-08-30T13:07:09.563",
            "2017-08-30T14:41:29.023"
        ],
        "Start": [
            "2017-08-29T14:36:12.42",
            "2017-08-30T12:59:53.05",
            "2017-08-30T14:40:45.34"
        ],
        "NumScrapsList": [
            0,
            3,
            1,
        ]
    }
}

outputs = [dict(zip(resp['Outputs'].keys(), e))
           for e in zip(*resp['Outputs'].values())]

pprint(outputs)

结果:

[{'Actual': '2017-08-29T14:37:47.137',
  'NumScrapsList': 0,
  'Start': '2017-08-29T14:36:12.42'},
 {'Actual': '2017-08-30T13:07:09.563',
  'NumScrapsList': 3,
  'Start': '2017-08-30T12:59:53.05'},
 {'Actual': '2017-08-30T14:41:29.023',
  'NumScrapsList': 1,
  'Start': '2017-08-30T14:40:45.34'}]

也许你可以尝试这样的事情:

response = {"Outputs": {
                  "Actual": [
                        "2017-08-29T14:37:47.137",
                        "2017-08-30T13:07:09.563",
                        "2017-08-30T14:41:29.023"
                     ],
                  "Start": [
                        "2017-08-29T14:36:12.42",
                        "2017-08-30T12:59:53.05",
                        "2017-08-30T14:40:45.34"
                     ],
                  "NumScrapsList": [0, 3, 4]
                 }
             }

temp = response["Outputs"]
final = {"Outputs": []}

for i in range(len(temp["NumScrapsList"])):
    entry = {"NumScrapsList": temp["NumScrapsList"][i],
             "Actual": temp["Actual"][i],
             "Start": temp["Start"][i]
            }
    final["Outputs"].append(entry)

print(final)

输出:

{   'Outputs': [   {   'NumScrapsList': 0,
                       'Actual': '2017-08-29T14:37:47.137',
                       'Start': '2017-08-29T14:36:12.42'
                   },
                   {   'NumScrapsList': 3,
                       'Actual': '2017-08-30T13:07:09.563',
                       'Start': '2017-08-30T12:59:53.05'
                   },
                   {   
                       'NumScrapsList': 4,
                       'Actual': '2017-08-30T14:41:29.023',
                       'Start': '2017-08-30T14:40:45.34'
                   }
               ]
}