0

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?

4

1 回答 1

0

返回的对象是一个真正的python dict:

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

所以你的问题真的是关于 python 字典。所以,不,这是不可能的。但是,您可以定义一个自定义,该自定义JSONDecoder可以返回您自己的实现您想要的语义的对象。

于 2013-03-26T16:38:14.830 回答