سؤال

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