2

我正在尝试在 python 中解析以下 json 文本,但出现错误,尽管我能够使用Json Viewer解析此 json 文本。所以我想我的 json 文本是正确的,有人可以帮我看看这里有什么问题吗?

import json as j

data = '{"c":[{"xy":{"xstart":0,"xend":5,"ystart":1,"yend":5},"names":["D","T","O","H","L","C",],"co":["rgb(0,0,128)"]}],"Values":{"D":["11/30/2012"],"T":["09:44:00"],"O":["5848.40"],"H":["5848.40"],"L":["5847.45"],"C":["5848.40"]}}'
json_data = j.loads(data)
#print json_data["c"][0]



Traceback (most recent call last):
  File "C:json\jsonexample.py", line 4, in <module>
    json_data = j.loads(data)
  File "C:\Python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
4

1 回答 1

4

看起来这是您的实际字符串的问题。这一行:

"names":["D","T","O","H","L","C",]

后面多了一个逗号"C"。尝试删除它,看看它是否按预期工作。

In [1]: import json as j

In [2]: data = '{"c":[{"xy":{"xstart":0,"xend":5,"ystart":1,"yend":5},"names":["D","T","O","H","L","C"],"co":["rgb(0,0,128)"]}],"Values":{"D":["11/30/2012"],"T":["09:44:00"],"O":["5848.40"],"H":["5848.40"],"L":["5847.45"],"C":["5848.40"]}}'

In [3]: json_data = j.loads(data)

In [4]: json_data['c'][0]
Out[4]:
{u'co': [u'rgb(0,0,128)'],
 u'names': [u'D', u'T', u'O', u'H', u'L', u'C'],
 u'xy': {u'xend': 5, u'xstart': 0, u'yend': 5, u'ystart': 1}}
于 2012-12-02T03:01:52.630 回答