【发布时间】:2019-04-02 05:15:17
【问题描述】:
我是 python 新手。 我有 PHP 索引数组。我正在使用 get 请求获取数据。
数据:
b'Array\n(\n [0] => HTTP/1.1 301 Moved Permanently\n [1] => Location: http://www.blahblah.com/\n [2] => HTTP/1.1 200 OK\n [3] => Date: Mon, 29 Oct 2018 09:29:10 GMT\n [4] => Server: blahblah\n [5] => Content-Length: 98836\n [6] => Last-Modified: Mon, 29 Oct 2018 08:13:21 UTC\n [7] => device_type: Touch\n [8] => content-language: en\n [9] => Set-Cookie: JSESSIONID=U-i_J9VsfqNazZl3uF4dAj2-7BmV370w58TXINO5UckAel4uGT5O!-115865558; path=/cs; HttpOnly\n [10] => Keep-Alive: timeout=300\n [11] => Connection: Keep-Alive\n [12] => Content-Type: text/html; charset=UTF-8\n [13] => Set-Cookie: NSC_q6.kjp.dpn_dmvtufs_WT.2*80=ffffffff0985006c45525d5f4f58455e445a4a422828;expires=Mon, 29-Oct-2018 09:31:10 GMT;path=/;httponly\n)\n\n'
我想转换成 json 对象。 预期输出:
{
"[0]" : "HTTP/1.1 301 Moved Permanently",
"[1]" : "Location: http://www.blahblah.com/",
"[2]" : "HTTP/1.1 200 OK"
"[3]" : "Date: Mon, 29 Oct 2018 09:29:10 GMT",
......,
.....,
}
我已经尝试过加载json的代码:
import requests
import json
u1="http://blahblah.com"
req=requests.get("http://gen.demotest.com/httpsProxy/httpsgetheaders.php?url=%s"%(u1))
data=req.content
print(json.dumps(data))
但出现了一些错误:
TypeError: Object of type bytes is not JSON serializable
【问题讨论】:
-
您需要解决此问题的发送方式。没有必要包含所有
Array\n...垃圾; PHP 应该发布表单编码或 JSON 数据。 (还要注意,您想要的输出不是 JSON 格式,而是 PHP;JSON 看起来像{"0": "HTTP...", ...}或者可能更好作为数组["HTTP...", ...])。 -
谢谢,@DanielRoseman 编辑了预期输出
-
如果你得到二进制作为输出,你必须在做 json.dumps 之前转换成字符串
标签: python arrays json python-3.x dictionary