python - What does "str indices must be integers" mean? -


i'm working dicts in jython created importing/parsing json. working sections see following message:

typeerror: str indices must integers 

this occurs when like:

if jsondata['foo']['bar'].lower() == 'baz':     ... 

where jsondata looks like:

{'foo': {'bar':'baz'} } 

what mean, , how fix it?

you need check type dict , existance of 'z' in dict before getting data dict.

>>> jsondata = {'a': '', 'b': {'z': true} } >>> key in jsondata: ...     if type(jsondata[key]) dict , 'z' in jsondata[key].keys() , jsondata[key]['z'] true: ...         print 'yes' ... yes >>> 

or shorter 1 dict.get

>>> jsondata = {'a': '', 'b': {'z': true}, 'c' :{'zz':true}} >>> key in jsondata: ...     if type(jsondata[key]) dict , jsondata[key].get('z',false): ...         print 'yes' ... yes >>> 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -