从dict python加载json

时间:2016-02-17 06:04:36

标签: json python-2.7

我正在尝试将字典对象转换为json,但无法执行此操作。我试过json.load(),json.loads()但它给了我以下错误:

File "lean.py", line 252, in createJSON
return json.loads(jsonElementDict)
File "/usr/lib/python2.7/json/__init__.py", line 338, 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())
TypeError: expected string or buffer

当我打印字典对象时,它如下:

{
'city': 'Pittsburgh',
'state': 'Pennsylvania',
'gross_floor_area': '',
'energyData': [
 {
  'cdd': '0.0', 
  'reading': '80.8', 
  'monthYear': 'Nov-2014'
 }, 
 {
  'cdd': '0.0',
  'reading': '300000.0', 
  'monthYear': 'Nov-2014'
 }
]
}`

代码:

outputJSON = json.loads(jsonElementDict)
print outputJSON.json['city']

0 个答案:

没有答案