【问题标题】:Error in parsing the json and formatting to pretty json解析 json 并格式化为漂亮的 json 时出错
【发布时间】:2017-05-05 23:45:23
【问题描述】:

我使用了 requests 模块,现在我以 json 格式取回了数据,并通过此How to Python prettyprint a JSON file 的帮助,我编写了我的代码,并且在执行代码时它给了我一个错误Expected a string or buffer,所以我改变了传递给解析器的变量为字符串。现在它又给出了另一个错误。

#Import
import requests
import json

r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r.status_code)
got_data_in_json = r.json()
parsed_json = json.loads(str(got_data_in_json))
print(json.dumps(parsed_json, indent=4 ,sort_keys=True))

错误日志:

python requests_post.py
200
Traceback (most recent call last):
  File "requests_post.py", line 8, in <module>
    parsed_json = json.loads(str(got_data_in_json))
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

有解决这个问题的办法吗?

【问题讨论】:

    标签: python json python-2.7 python-3.x python-requests


    【解决方案1】:

    r.json() 返回 json

    所以你不需要json.loads(str(got_data_in_json))

    import requests
    import json
    
    r = requests.post('http://httpbin.org/post', data = {'key':'value'})
    print(r.status_code)
    got_data_in_json = r.json()
    print(json.dumps(got_data_in_json, indent=4 ,sort_keys=True))
    

    输出:

    200
    {
        "args": {},
        "data": "",
        "files": {},
        "form": {
            "key": "value"
        },
        "headers": {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Content-Length": "9",
            "Content-Type": "application/x-www-form-urlencoded",
            "Host": "httpbin.org",
            "User-Agent": "python-requests/2.11.1"
        },
        "json": null,
        "origin": "103.227.98.245",
        "url": "http://httpbin.org/post"
    }
    

    【讨论】:

      【解决方案2】:

      r.json() 为您解析 JSON 并返回 Python 数据结构。无需针对该数据调用json.loads()

      【讨论】:

        【解决方案3】:

        ValueError:预期属性名称:第 1 行第 2 列(字符 1)

        问题是,str(got_data_in_json) 不是一个有效的 JSON

        In [2]: str(got_data_in_json)
        Out[2]: "{u'files': {}, u'origin': u'50.57.61.145', u'form': {u'key': u'value'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'9', u'Accept-Encoding': u'gzip, deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.11.1', u'Host': u'httpbin.org', u'Content-Type': u'application/x-www-form-urlencoded'}, u'json': None, u'data': u''}"
        

        got_data_in_json 已经是一个可以转储的 Python 数据结构:

        got_data_in_json = r.json()
        print(json.dumps(got_data_in_json, indent=4, sort_keys=True))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-18
          • 2021-09-19
          • 2015-12-03
          • 2013-09-23
          • 1970-01-01
          • 1970-01-01
          • 2014-12-13
          • 1970-01-01
          相关资源
          最近更新 更多