I can't figure out how to perform nested object access using simplejson. I have looked at the examples in the docs and searched but can't find a way of achieving the following functionality:

nested = json.loads('{ "foo": {"bar": 1}}')
value = nested['foo.bar']
if(value == 1):
  print('success')

This produces the following error:

KeyError: 'foo.bar'

Is there a way of getting nested objects without having to access one object at a time?

有帮助吗?

解决方案

The object returned is a true python dict:

>>> type(nested)
<type 'dict'>

so really your question is about python dictionaries. So, no, this cannot be done. It would be possible for you to define a custom JSONDecoder which could return your own object that implements the semantics you want, however.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top