如何将json响应转换为Python列表

时间:2015-03-16 06:35:07

标签: python python-3.x pandas

我按requests.get

收到JSON回复
req = requests.get(SAMPLE_SCHEDULE_API)

并将其转换为字典

data = json.loads(req.text)["data"]

当我尝试将字符串转换为Python dict时,

我得到了ValueError: malformed node or string:

ast.literal_eval(data)

我不知道如何完成这项任务。

代码段

    def schedules(cls, start_date=None, end_date=None):
        import ast
        req = requests.get(SAMPLE_SCHEDULE_API)
        data = json.loads(req.text)["data"]
        ast.literal_eval(data)
        return pd.DataFrame(json.loads(req.text)["data"])

JSON响应

{
status: "ok",
version: "v1",
data: "[
{"_id":"2015-01-28","end_date":"2015-01-28","estimated_release":1422453600000,"is_projection":false,"is_statement":true,"material_link":null,"start_date":"2015-01-27"},
{"_id":"2015-03-18","end_date":"2015-03-18","estimated_release":1426687200000,"is_projection":false,"is_statement":false,"material_link":null,"start_date":"2015-03-17"},
{"_id":"2015-04-29","end_date":"2015-04-29","estimated_release":1430316000000,"is_projection":false,"is_statement":false,"material_link":null,"start_date":"2015-04-28"},
{"_id":"2015-06-17","end_date":"2015-06-17","estimated_release":1434549600000,"is_projection":false,"is_statement":false,"material_link":null,"start_date":"2015-06-16"},
{"_id":"2015-07-29","end_date":"2015-07-29","estimated_release":1438178400000,"is_projection":false,"is_statement":false,"material_link":null,"start_date":"2015-07-28"}]"
}

详细信息错误消息

Traceback (most recent call last):
  File "fomc.py", line 25, in <module>
    schedules = FOMC.schedules()
  File "fomc.py", line 21, in schedules
    ast.literal_eval(data)
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 86, in literal_eval
    return _convert(node_or_string)
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 58, in _convert
    return list(map(_convert, node.elts))
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/ast.py", line 85, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x10a19c990> 

3 个答案:

答案 0 :(得分:2)

您已对data进行了两次编码(严格来说不是必需的)。您只需要使用data再次解码json.loads

def schedules(cls, start_date=None, end_date=None):
    req = requests.get(SAMPLE_SCHEDULE_API)
    data_json = json.loads(req.text)["data"]
    data = json.loads(data_json)
    return pd.DataFrame(data)

请注意ast.literal_eval用于Python代码,而json.loads用于紧跟JavaScript代码的JSON;例如,truefalsenullTrueFalseNone存在差异。前者是JSON中使用的javascript语法(因此您需要json.loads;后者是Python代码,您可以使用ast.literal_eval

答案 1 :(得分:0)

字段&#34;数据&#34;是一个字符串,而不是一个列表。该字符串的内容似乎也是JSON,因此您出于某种原因将JSON封装在JSON中。如果可以,请修复它,以便只编码为JSON一次。如果这不起作用,您可以检索该字段并单独解码。

答案 2 :(得分:0)

由于响应已经是json格式,因此您无需对其进行编码。像这样的方法,

req = requests.get(SAMPLE_SCHEDULE_API)
data_str = req.json().get('data')
json_data = json.loads(data_str)

json()方法将返回响应的json编码内容。