Simplejson转储和加载不返回有效的字典

Wil*_*ran 2 python google-app-engine json simplejson

我正在尝试将json结果存储在GAE数据存储区中,以便我以后可以读取它.我将它转储到String,然后存储它,然后读取它并将其加载回dict.但是在装载之后我再也不能把它看成dict了.

result = freebase.mqlready(query)
Run Code Online (Sandbox Code Playgroud)

打印结果:

[{u'mid': u'/m/095hd',
  u'name': u'Settlers of Catan',
  u'type': u'/games/game'},
 {u'mid': u'/m/025sm93',
  u'name': u'The Game of Life',
  u'type': u'/games/game'}]
Run Code Online (Sandbox Code Playgroud)

 

for r in result:
    name = r.name # works, I can get the name and other values.

json_dump = simplejson.dumps(result)
text = db.Text(json_dump)
fbresult = model.FB(text=text)
fbresult.put()
####
stored_text = fbresult.text
json = simplejson.loads(stored_text)
Run Code Online (Sandbox Code Playgroud)

打印json:

[{u'mid': u'/m/095hd',
  u'name': u'Settlers of Catan',
  u'type': u'/games/game'},
 {u'mid': u'/m/025sm93',
  u'name': u'The Game of Life',
  u'type': u'/games/game'}]
Run Code Online (Sandbox Code Playgroud)

 

for j in json:
    name = json.name 
Run Code Online (Sandbox Code Playgroud)

错误:

AttributeError: 'dict' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)

Tyl*_*ves 9

呃,看起来你正在访问集合而不是内部对象:

当然你的意思是:

for j in json:
    name = j['name']
Run Code Online (Sandbox Code Playgroud)