用python中的json.loads解码json字符串

时间:2017-06-05 21:14:58

标签: python json

我有这样的字符串:

jstring = {"label":"2017-06-01","value1":"250.730000"},{"label":"2017-06-02","value1":"250.730000"}

如果我使用json.loads(jstring),我收到此错误:

Traceback (most recent call last):
  File "Funds.py", line 44, in <module>
    data  = json.loads(array)
  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 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 45 - line 1 column 54951 (char 44 - 54950)

我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果像这样在字符串上有整个东西:

jstring = '{"label":"2017-06-01","value1":"250.730000"},{"label":"2017-06-02","value1":"250.730000"}'

然后转储过程将此视为两个不同的对象,它无法正确处理信息。你需要把你的字符串放在一个像这样的对象中:

jstring = '{"first":{"label":"2017-06-01","value1":"250.730000"},"second":{"label":"2017-06-02","value1":"250.730000"}}'

或者像这样:

jstring = '[{"label":"2017-06-01","value1":"250.730000"},{"label":"2017-06-02","value1":"250.730000"}]'

解析器只能看到一个对象