Json格式化,python解析

时间:2014-08-08 17:11:41

标签: python json parsing

我有一个看起来像这样的json文件:

{'data':[-99,19,1212,121,2131,323321,123]}

保存为list.json

我有一个需要列表[-99,19,1212,121,2131,323321,123]

的python脚本
 import json

 with open ("list.json", "r") as myfile:
        data=myfile.read().replace('\n', '')
 spots1 = json.loads(data) #<-- Error
 spots = spots1['data']

错误:

File "pythonscript.py", line 479, in <module>
    spots1 = json.loads(data)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)

我无法弄清楚格式化有什么问题。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

Python希望你在json字典中使用“(双引号)属性名称而不是'(单引号)

试试这个字符串: { “数据”:[ - 99,19,1212,121,2131,323321,123]}

有关详细信息,请参阅此问题:python: single vs double quotes in JSON