【问题标题】:how can i Extract JSON from HTTP Response (python)我如何从 HTTP 响应中提取 JSON (python)
【发布时间】:2021-07-13 01:56:12
【问题描述】:

我从 lambda api 网关得到这个。我怎样才能得到 {\n "a":"b",\n "c":"d",\n "e":"f",\n "g":"h"\n} 到 json 风格?

"------WebKitFormBoundarySfqiG2UABUZYF7Ir\r\n
Content-Disposition: form-data; 
name=\"upload-file\"; 
filename=\"example.json\"\r\n
Content-Type: application/json\r\n\r\n
{\n \"a\":\"b\",\n \"c\":\"d\",\n \"e\":\"f\",\n \"g\":\"h\"\n}
\r\n------WebKitFormBoundarySfqiG2UABUZYF7Ir--"

我想做这个 {\n "a":"b",\n "c":"d",\n "e":"f",\n "g":"h"\n}


{
  "a":"b",
  "c":"d",
  "e":"f",
  "g":"h"
}

【问题讨论】:

    标签: python json lambda http-headers httprequest


    【解决方案1】:

    您如何处理响应以获取此信息?

    试试这个

    import json
    result = json.loads(x[x.find('{'): x.find('}')+1])
    

    x 是你的字符串

    【讨论】:

    • 我从 event['body-json'] 得到这个。我会试试的
    • 我更新代码,这不是最好的方法,但可以工作。