【发布时间】:2015-03-15 11:37:06
【问题描述】:
我有以下 JSON 字典:
{
u'period': 16, u'formationName': u'442', u'formationId': 2,
u'formationSlots': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0],
u'jerseyNumbers': [1, 20, 3, 15, 17, 5, 19, 6, 18, 25, 10, 2, 4, 12, 16, 22, 24,
34],
u'playerIds': [23122, 38772, 24148, 39935, 29798, 75177, 3860, 8505,
26013, 3807, 34693, 18181, 4145, 23446, 8327, 107395, 29762, 254558],
u'captainPlayerId': 29798,
u'startMinuteExpanded': 0,
u'endMinuteExpanded': 82,
u'formationPositions': [{u'horizontal': 5.0, u'vertical': 0.0},
{u'horizontal': 1.0, u'vertical': 2.5}, {u'horizontal': 9.0, u'vertical': 2.5},
{u'horizontal':3.5, u'vertical': 6.0}, {u'horizontal': 3.5, u'vertical': 2.5},
{u'horizontal': 6.5, u'vertical': 2.5}, {u'horizontal': 1.0, u'vertical': 6.0},
{u'horizontal': 6.5, u'vertical': 6.0}, {u'horizontal': 6.5, u'vertical': 9.0},
{u'horizontal': 3.5, u'vertical': 9.0}, {u'horizontal': 9.0, u'vertical': 6.0}]
}
如您所见,一些字典值包含在列表中。我正在尝试以编程方式从该对象中获取所有值,如下所示:
for myvalue in myjsonobject:
print mydict
for mysubvalue in myvalue:
print mysubvalue
这会打印字典键:
period
formationName
formationId
formationSlots
jerseyNumbers
playerIds
captainPlayerId
startMinuteExpanded
endMinuteExpanded
formationPositions
当我真正想要的是价值观时。我尝试用 print mysubvalue.values() 替换行 print mysubvalue,但这会导致以下错误:
Traceback (most recent call last):
File "C:\Python27\counter.py", line 78, in <module>
print mysubdict.values()
AttributeError: 'unicode' object has no attribute 'values'
我在这里进行有根据的猜测,我不需要使用json.loads(mysubdict) 来允许我访问.values() 函数。如果是这样,我不确定为什么会收到此错误。
有人可以帮忙吗?
谢谢
【问题讨论】:
标签: python json dictionary