在从存储抽象语法树的 JSON 中提取信息方面需要帮助
Need help in Extracting information from an JSON which is storing an Abstract Syntax Tree
{
"id": "9",
"children": [{
"id": "8",
"children": [{
"id": "7",
"children": [{
"id": "6",
"children": [
{
"id": "0",
"type": "isPathForward"
},
{
"id": "2",
"children": [{
"id": "1",
"type": "maze_moveForward"
}],
"type": "DO"
},
{
"id": "5",
"children": [{
"id": "4",
"children": [{
"id": "3",
"type": "turnLeft"
}],
"type": "maze_turn"
}],
"type": "ELSE"
}
],
"type": "maze_ifElse"
}],
"type": "DO"
}],
"type": "maze_forever"
}],
"type": "program"
}
上面的有效 JSON 基本上是一个 AST(抽象语法树),我只想按顺序提取 "type" :1) 节点后跟 2) 左 child 然后 3) 右 child
与上面的完全一样 JSON :
Program
maze_forever
DO
maze_ifElse
isPathforward
Do
maze_moveForward
Else
maze_turn
turn_Left
我没有使用过 json 我尝试在 python 中使用生成器,但是在转换为字典的过程中顺序丢失了。
你能为此写一个 python 实现吗?
更新 !!!!
到目前为止我已经尝试过:
进口json
json_string=json.loads(以上json)
when i type :
for i in json_string:
... print(i)
...
OUTPUT
type
id
children
我也试过了
import pandas as pd
d=pd.read_json('{ "id": "9", "children": [{ "id": "8", "children": [{ "id": "7", "children": [{ "id": "6", "children": [ { "id": "0", "type": "isPathForward" }, { "id": "2", "children": [{ "id": "1", "type": "maze_moveForward" }], "type": "DO" }, { "id": "5", "children": [{ "id": "4", "children": [{ "id": "3", "type": "turnLeft" }], "type": "maze_turn" }], "type": "ELSE" } ], "type": "maze_ifElse" }], "type": "DO" }], "type": "maze_forever" }], "type": "program"}')
>>> d
output :
children id type
0 {'type': 'maze_forever', 'id': '8', 'children'... 9 program
以上两种情况:
我不知道如何递归进入 children,因为每个 child 里面都有一个或多个 children。我搜索的大多数答案都没有解释与我上面的 JSON 一样嵌套的 JSON。
最明显的实现是递归函数:
>>> def process(data):
... if 'type' in data: print data['type']
... if 'children' in data:
... for child in data['children']:
... process(child)
...
>>> j = json.load(open('test.json', 'r'))
>>> process(j)
program
maze_forever
DO
maze_ifElse
isPathForward
DO
maze_moveForward
ELSE
maze_turn
turnLeft
请注意,我们正在打印当前结构的类型,然后我们递归到子级。
{
"id": "9",
"children": [{
"id": "8",
"children": [{
"id": "7",
"children": [{
"id": "6",
"children": [
{
"id": "0",
"type": "isPathForward"
},
{
"id": "2",
"children": [{
"id": "1",
"type": "maze_moveForward"
}],
"type": "DO"
},
{
"id": "5",
"children": [{
"id": "4",
"children": [{
"id": "3",
"type": "turnLeft"
}],
"type": "maze_turn"
}],
"type": "ELSE"
}
],
"type": "maze_ifElse"
}],
"type": "DO"
}],
"type": "maze_forever"
}],
"type": "program"
}
上面的有效 JSON 基本上是一个 AST(抽象语法树),我只想按顺序提取 "type" :1) 节点后跟 2) 左 child 然后 3) 右 child
与上面的完全一样 JSON :
Program
maze_forever
DO
maze_ifElse
isPathforward
Do
maze_moveForward
Else
maze_turn
turn_Left
我没有使用过 json 我尝试在 python 中使用生成器,但是在转换为字典的过程中顺序丢失了。
你能为此写一个 python 实现吗?
更新 !!!!
到目前为止我已经尝试过:
进口json json_string=json.loads(以上json)
when i type :
for i in json_string:
... print(i)
...
OUTPUT
type
id
children
我也试过了
import pandas as pd
d=pd.read_json('{ "id": "9", "children": [{ "id": "8", "children": [{ "id": "7", "children": [{ "id": "6", "children": [ { "id": "0", "type": "isPathForward" }, { "id": "2", "children": [{ "id": "1", "type": "maze_moveForward" }], "type": "DO" }, { "id": "5", "children": [{ "id": "4", "children": [{ "id": "3", "type": "turnLeft" }], "type": "maze_turn" }], "type": "ELSE" } ], "type": "maze_ifElse" }], "type": "DO" }], "type": "maze_forever" }], "type": "program"}')
>>> d
output :
children id type
0 {'type': 'maze_forever', 'id': '8', 'children'... 9 program
以上两种情况:
我不知道如何递归进入 children,因为每个 child 里面都有一个或多个 children。我搜索的大多数答案都没有解释与我上面的 JSON 一样嵌套的 JSON。
最明显的实现是递归函数:
>>> def process(data):
... if 'type' in data: print data['type']
... if 'children' in data:
... for child in data['children']:
... process(child)
...
>>> j = json.load(open('test.json', 'r'))
>>> process(j)
program
maze_forever
DO
maze_ifElse
isPathForward
DO
maze_moveForward
ELSE
maze_turn
turnLeft
请注意,我们正在打印当前结构的类型,然后我们递归到子级。