将字节对象转换为python字典或json对象

时间:2018-10-29 09:45:25

标签: python arrays json python-3.x dictionary

我是python的新手。  我有PHP索引数组。使用获取请求,我正在获取数据。

数据:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

我想转换为json对象。 预期输出:

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",
......,
.....,

但是出现了一些错误:

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))

1 个答案:

答案 0 :(得分:-1)

我认为您必须将二进制响应转换为文本。以下几行会做到这一点。

data=json.loads(req.text)
print(data)