无法在json响应中访问float / int - PYTHON

时间:2018-01-26 15:21:43

标签: python json

我希望从响应的timing部分访问值。

我有以下JSON响应。

    {
        "method": "Network.responseReceived",
        "params": {
            "frameId": "(297304CB88DA6BF7ED17760594B93F3E)",
            "loaderId": "9976.1",
            "requestId": "9976.6",
            "response": {
                "connectionId": 84,
                "connectionReused": false,
                "encodedDataLength": 453,
                "fromDiskCache": false,
                "fromServiceWorker": false,
                "headers": {
                    "Cache-Control": "max-age=2592000",
                    "Connection": "keep-alive",
                    "Content-Encoding": "gzip",
                    "Content-Type": "text/css",
                    "Date": "Thu, 25 Jan 2018 19:47:50 GMT",
                    "Expires": "Sat, 24 Feb 2018 19:47:50 GMT",
                    "Last-Modified": "Fri, 20 Oct 2017 22:53:18 GMT",
                    "Pragma": "public",
                    "Server": "nginx",
                    "Transfer-Encoding": "chunked",
                    "Vary": "Accept-Encoding",
                    "X-Content-Type-Options": "nosniff",
                    "X-Nginx-Cache-Status": "HIT",
                    "X-Server-Powered-By": "Engintron",
                    "X-XSS-Protection": "1; mode=block"
                },
                "headersText": "HTTP/1.1 200 OK\r\nServer: nginx\r\nDate: Thu, 25 Jan 2018 19:47:50 GMT\r\nContent-Type: text/css\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nVary: Accept-Encoding\r\nLast-Modified: Fri, 20 Oct 2017 22:53:18 GMT\r\nExpires: Sat, 24 Feb 2018 19:47:50 GMT\r\nCache-Control: max-age=2592000\r\nX-XSS-Protection: 1; mode=block\r\nX-Content-Type-Options: nosniff\r\nX-Nginx-Cache-Status: HIT\r\nX-Server-Powered-By: Engintron\r\nPragma: public\r\nContent-Encoding: gzip\r\n\r\n",
                "mimeType": "text/css",
                "protocol": "http/1.1",
                "remoteIPAddress": "213.251.187.58",
                "remotePort": 80,
                "requestHeaders": {
                    "Accept": "text/css,*/*;q=0.1",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "en-US,en;q=0.9",
                    "Connection": "keep-alive",
                    "Host": "www.oostcircle.nl",
                    "Referer": "http://example",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
                },
                "requestHeadersText": "GET /static/personal/css/style.min.css HTTP/1.1\r\nHost: example.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\nAccept: text/css,*/*;q=0.1\r\nReferer: http://example.com/\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n",
                "securityState": "neutral",
                "status": 200,
                "statusText": "OK",
                "timing": {
                    "connectEnd": 25.7599998731166,
                    "connectStart": 13.7589999940246,
                    "dnsEnd": 13.7589999940246,
                    "dnsStart": 3.55999986641109,
                    "proxyEnd": -1,
                    "proxyStart": -1,
                    "pushEnd": 0,
                    "pushStart": 0,
                    "receiveHeadersEnd": 45.4459998290986,
                    "requestTime": 1296269.240561,
                    "sendEnd": 26.484000030905,
                    "sendStart": 26.1399999726564,
                    "sslEnd": -1,
                    "sslStart": -1,
                    "workerReady": -1,
                    "workerStart": -1
                },
                "url": "http://www.example.com/static/personal/css/style.min.css"
            },
            "timestamp": 1296269.289171,
            "type": "Stylesheet"
        }
    },    

我已成功提取typeurl值,但当我尝试使用以下内容提取时间时:

with open(file_) as json_file:  
    data = json.load(json_file, parse_int=Decimal, parse_float=Decimal)
    for p in data:
        if "Network.responseReceived" in p['method']:

            print('URL: ' + p['params']['response']['timing']['pushEnd'])

使用Python文档https://docs.python.org/3/library/json.html

中指定的十进制解析器

我收到错误:

TypeError: must be str, not decimal.Decimal

没有解析器我得到:

TypeError: must be str, not int

我尝试过使用[str('pushEnd')]并收到同样的错误。

如何从JSON

访问和返回Integers和Floats

完整回溯是:

Traceback (most recent call last):
  File "e:/calc_time.py", line 17, in <module>
     print('URL: ' + p['params']['response']['timing'][str('pushEnd')])
TypeError: must be str, not int

1 个答案:

答案 0 :(得分:0)

正如我所提到的,我根本不明白你为什么要解析整数和浮点数作为小数。但无论哪种方式,这都不是你所遇到的问题的原因,因为无论是否有错误。

错误回溯显示错误的原因:您正在尝试将字符串与int或小数连接起来。 Python不会让你这样做;你只能将字符串与其他字符串连接起来。

有多种方法可以解决这个问题,但最好的方法是不要连接。改为使用字符串插值。

print('URL: {}'.format(p['params']['response']['timing']['pushEnd']))

(注意,您尝试通过执行p...[str('pushEnd')]来解决这个问题根本不是正确的;它不是关键,&#34; pushEnd&#34;,这就是问题所在,以及无论如何,它已经是一个字符串;它是值,即该查找的结果,这是一个int。所以你可以做str(p....['pushEnd']),但就像我一样说使用插值更好。)