문제

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