从JSON
导入可以得到非常复杂的嵌套结构。
例如:
{u'body': [{u'declarations': [{u'id': {u'name': u'i',
u'type': u'Identifier'},
u'init': {u'type': u'Literal', u'value': 2},
u'type': u'VariableDeclarator'}],
u'kind': u'var',
u'type': u'VariableDeclaration'},
{u'declarations': [{u'id': {u'name': u'j',
u'type': u'Identifier'},
u'init': {u'type': u'Literal', u'value': 4},
u'type': u'VariableDeclarator'}],
u'kind': u'var',
u'type': u'VariableDeclaration'},
{u'declarations': [{u'id': {u'name': u'answer',
u'type': u'Identifier'},
u'init': {u'left': {u'name': u'i',
u'type': u'Identifier'},
u'operator': u'*',
u'right': {u'name': u'j',
u'type': u'Identifier'},
u'type': u'BinaryExpression'},
u'type': u'VariableDeclarator'}],
u'kind': u'var',
u'type': u'VariableDeclaration'}],
u'type': u'Program'}
对于上述复杂结构,推荐的行走方式是什么?
除了一些清单外,大部分是字典,结构可能变得更加叠瓦,所以我需要一个一般的解决方案。
如果只需要遍历字典,我建议使用递归
walk
函数,该函数接受字典,然后递归遍历其元素。像这样的:如果您还想搜索元素,或者查询几个通过特定条件的元素,请查看jsonpath模块。
可以使用递归生成器将字典转换为平面列表。
它又回来了
更新:修复了注释中提到的从
[key] + pre
到pre + [key]
的键列表。根据任务的不同,您可以从标准库
json
模块扩展编码器和解码器,而不是编写自己的解析器。我建议您尤其是在需要将属于自定义类的对象编码到json中时。如果必须对json的字符串表示执行某些操作,还可以考虑迭代JSONEncoder().iterencode
对于这两个引用都是http://docs.python.org/2/library/json.html#encoders-and-decoders
相关问题 更多 >
编程相关推荐