Python AttributeError: 'unicode' 对象在解析 JSON 字典值时没有属性 'values'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27991180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'unicode' object has no attribute 'values' when parsing JSON dictionary values
提问by gdogg371
I have the following JSON dictionary:
我有以下 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}]
}
As you can see some of the dictionary values are contained within lists. I am trying to obtain all the values from this object programatically like so:
如您所见,某些字典值包含在列表中。我正在尝试以编程方式从该对象中获取所有值,如下所示:
for myvalue in myjsonobject:
print mydict
for mysubvalue in myvalue:
print mysubvalue
This prints the dictionary keys:
这将打印字典键:
period
formationName
formationId
formationSlots
jerseyNumbers
playerIds
captainPlayerId
startMinuteExpanded
endMinuteExpanded
formationPositions
When what I actually want is the values. I have tried replacing the line print mysubvalue
with print mysubvalue.values()
, however this causes the following error:
当我真正想要的是价值观时。我曾尝试更换联机打印mysubvalue
与印刷mysubvalue.values()
,但是这将导致以下错误:
Traceback (most recent call last):
File "C:\Python27\counter.py", line 78, in <module>
print mysubdict.values()
AttributeError: 'unicode' object has no attribute 'values'
I'm taking an educated guess here that I dont need to use json.loads(mysubdict)
to allow me access the .values()
function. If so, I am not sure why I am getting this error.
我在这里进行了有根据的猜测,我不需要使用它json.loads(mysubdict)
来允许我访问该.values()
功能。如果是这样,我不确定为什么会收到此错误。
Can anyone assist?
任何人都可以提供帮助吗?
Thanks
谢谢
采纳答案by Ranvijay Sachan
If you iterate over the dictionary itself (for myvalue in myjsonobject), you will be iterating over the keys of the dictionary. When looping with a for loop, the behavior will be the same whether you loop over the dict
(myjsonobject) itself, myjsonobject.keys()
, or myjsonobject.iterkeys()
. dict.iterkeys()
is generally preferable because it is explicit and efficient:
如果您遍历字典本身(对于 myjsonobject 中的 myvalue),您将遍历字典的键。使用 for 循环进行循环时,无论您循环dict
(myjsonobject) 本身myjsonobject.keys()
、 还是,行为都将相同myjsonobject.iterkeys()
。dict.iterkeys()
通常更可取,因为它明确且高效:
for myvalue in myjsonobject.iterkeys():
for myvalue in myjsonobject.iterkeys():
回答by Andrew
You're iterating over the keys of the JSON dictionary, and then calling .values() on each key.
您正在遍历 JSON 字典的键,然后对每个键调用 .values()。
for myvalue in myjsonobject:
iterates over the keys. So when you get to a key that is a string, let's say, u'period' : 16, it will print 'period'.values(), which spits out that error as the string class has no .values().
迭代键。所以当你得到一个字符串的键时,比如 u'period' : 16,它会打印 'period'.values(),因为字符串类没有 .values(),它会抛出那个错误。
If you want to flatten the entire JSON dictionary to an arbitrary depth, I would suggest a recursive approach.
如果您想将整个 JSON 字典展平到任意深度,我建议采用递归方法。