访问JSON树的叶子节点

3

I have a JSON file of the form:

{"id":442500000116137984, "reply":0, "children":[{"id":442502378957201408, "reply":0, "children":[]}]}
{"id":442500001084612608, "reply":0, "children":[{"id":442500145871990784, "reply":1, "children":[{"id":442500258421952512, "reply":1, "children":[]}]}]}
{"id":442500000258342912, "reply":0, "children":[{"id":442500636668489728, "reply":0, "children":[]}]}

每行代表一棵独立的树。现在我想要进入每棵树的叶子节点并执行某些操作,基本上是这样。

import json
f = open("file", 'r')
for line in f:
    tree = json.loads(line)
    #somehow walk through the tree and find leaves
    if isLeaf(child):
        print "Reached Leaf"

如何遍历这个树形对象以检测所有的叶子节点?
3个回答

4

这应该可以正常工作。

import json
f = open("file", 'r')

leafArray = []

def parseTree(obj):
    if len(obj["children"]) == 0:
        leafArray.append(obj)
    else:
        for child in obj["children"]:
            parseTree(child)

for line in f:
    global leafArray
    leafArray = []
    tree = json.loads(line.strip())
    parseTree(tree) 
    #somehow walk through the tree and find leaves
    print ""
    for each in leafArray:
        print each

2
你知道吗,我曾经需要处理很多JSON中的超媒体对象,所以我写了这个库。问题是我事先不知道树的深度,所以我需要能够搜索并获取我所谓的“路径”(到达叶子节点所使用的键/索引集合)和值。
无论如何,你可以从中获得一些想法(我只为Python3.3+编写了它,但这里有一个类内部的方法可以完成你想要的功能)。
基本思路是沿着树向下走,并检查遇到的对象。如果你遇到更多的字典(甚至在列表中),你就会继续深入(我发现将其编写为递归生成器更容易,主要是通过子类化collections.MutableMapping并创建具有自定义enumerate的类来完成)。
你会跟踪你沿途走过的路径,一旦你获得一个不值得进一步探索的值(它不是一个dictlist),那么你就会产生你的路径和值:
def enumerate(self, path=None):
    """Iterate through the PelicanJson object yielding 1) the full path to
    each value and 2) the value itself at that path.
    """
    if path is None:
    path = []
    for k, v in self.store.items():
        current_path = path[:]
        current_path.append(k)

        if isinstance(v, PelicanJson):
            yield from v.enumerate(path=current_path)
        elif isinstance(v, list):
            for idx, list_item in enumerate(v):
                list_path = current_path[:]
                list_path.append(idx)
                if isinstance(list_item, PelicanJson):
                    yield from list_item.enumerate(path=list_path)
                else:
                    yield list_path, list_item
        else:
            yield current_path, v

因为这个库是专门为Python3设计的,所以它利用了像yield from这样的特性,因此它不能直接为您工作(我当然不是要提供我的解决方案作为唯一的解决方案)。就个人而言,在各种函数中重复使用大量逻辑让我感到沮丧,因此编写这个库节省了我很多工作,我可以回到处理超媒体API时做一些奇怪的事情。



-1
你可以这样做。(我不知道Python的语法。)
temp = tree #Your JSON object in each line
while (temp.children ! = []){
    temp = temp.children;
}

你的 temp 现在将成为叶子。

谢谢,但我真的在寻找Python的答案...因为我自己也是一个Python新手。 - akhiljain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接