gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:46:02 25 4
gpt4 key购买 nike

我正在尝试将 json 结果存储在 GAE 数据存储中,以便稍后读取。我将它转储到一个字符串中,然后存储它,然后读取它并将其加载回字典中。但加载后我无法再将其作为字典阅读。

result = freebase.mqlready(query)

打印结果:

[{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'}]

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)

打印 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'}]

for j in json:
name = json.name

错误:

AttributeError: 'dict' object has no attribute 'name'

最佳答案

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

当然你的意思是:

for j in json:
name = j['name']

关于python - Simplejson 转储和加载不返回有效字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4582636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com