使用JSPath库解析json并返回对象

时间:2016-12-21 16:11:59

标签: javascript json node.js node-modules

我正在遍历一个JSON对象。这是我的JSON

{
    "type": "Compound",
    "body": [
        {
            "type": "CallExpression",
            "arguments": [
                {
                    "type": "CallExpression",
                    "arguments": [],
                    "callee": {
                        "type": "MemberExpression",
                        "computed": false,
                        "object": {
                            "type": "MemberExpression",
                            "computed": false,
                            "object": {
                                "type": "Identifier",
                                "name": "Employee"
                            },
                            "property": {
                                "type": "Identifier",
                                "name": "Name"
                            }
                        },
                        "property": {
                            "type": "Identifier",
                            "name": "trim"
                        }
                    }
                }
            ],
            "callee": {
                "type": "MemberExpression",
                "computed": false,
                "object": {
                    "type": "Literal",
                    "value": "hello",
                    "raw": "'hello'"
                },
                "property": {
                    "type": "Identifier",
                    "name": "concat"
                }
            }
        },
        {
            "type": "CallExpression",
            "arguments": [],
            "callee": {
                "type": "MemberExpression",
                "computed": false,
                "object": {
                    "type": "CallExpression",
                    "arguments": [
                        {
                            "type": "Literal",
                            "value": "Hello",
                            "raw": "\"Hello\""
                        }
                    ],
                    "callee": {
                        "type": "Identifier",
                        "name": "and"
                    }
                },
                "property": {
                    "type": "Identifier",
                    "name": "trim"
                }
            }
        },
        {
            "type": "Identifier",
            "name": "are"
        },
        {
            "type": "CallExpression",
            "arguments": [],
            "callee": {
                "type": "MemberExpression",
                "computed": false,
                "object": {
                    "type": "CallExpression",
                    "arguments": [
                        {
                            "type": "Literal",
                            "value": "hello",
                            "raw": "'hello'"
                        }
                    ],
                    "callee": {
                        "type": "Identifier",
                        "name": "functions"
                    }
                },
                "property": {
                    "type": "Identifier",
                    "name": "trim"
                }
            }
        },
        {
            "type": "Identifier",
            "name": "for"
        },
        {
            "type": "Identifier",
            "name": "expression"
        },
        {
            "type": "CallExpression",
            "arguments": [
                {
                    "type": "Identifier",
                    "name": "fg1"
                }
            ],
            "callee": {
                "type": "MemberExpression",
                "computed": false,
                "object": {
                    "type": "CallExpression",
                    "arguments": [
                        {
                            "type": "Literal",
                            "value": "Hello",
                            "raw": "'Hello'"
                        }
                    ],
                    "callee": {
                        "type": "CallExpression",
                        "arguments": [],
                        "callee": {
                            "type": "MemberExpression",
                            "computed": false,
                            "object": {
                                "type": "CallExpression",
                                "arguments": [
                                    {
                                        "type": "Literal",
                                        "value": "Hello",
                                        "raw": "'Hello'"
                                    }
                                ],
                                "callee": {
                                    "type": "CallExpression",
                                    "arguments": [
                                        {
                                            "type": "Literal",
                                            "value": "hello",
                                            "raw": "'hello'"
                                        }
                                    ],
                                    "callee": {
                                        "type": "MemberExpression",
                                        "computed": false,
                                        "object": {
                                            "type": "CallExpression",
                                            "arguments": [
                                                {
                                                    "type": "Literal",
                                                    "value": "Hello",
                                                    "raw": "'Hello'"
                                                }
                                            ],
                                            "callee": {
                                                "type": "CallExpression",
                                                "arguments": [],
                                                "callee": {
                                                    "type": "MemberExpression",
                                                    "computed": false,
                                                    "object": {
                                                        "type": "CallExpression",
                                                        "arguments": [
                                                            {
                                                                "type": "Literal",
                                                                "value": "Hello",
                                                                "raw": "'Hello'"
                                                            }
                                                        ],
                                                        "callee": {
                                                            "type": "CallExpression",
                                                            "arguments": [],
                                                            "callee": {
                                                                "type": "MemberExpression",
                                                                "computed": false,
                                                                "object": {
                                                                    "type": "CallExpression",
                                                                    "arguments": [
                                                                        {
                                                                            "type": "Literal",
                                                                            "value": "Hello",
                                                                            "raw": "'Hello'"
                                                                        }
                                                                    ],
                                                                    "callee": {
                                                                        "type": "Identifier",
                                                                        "name": "builder"
                                                                    }
                                                                },
                                                                "property": {
                                                                    "type": "Identifier",
                                                                    "name": "trim"
                                                                }
                                                            }
                                                        }
                                                    },
                                                    "property": {
                                                        "type": "Identifier",
                                                        "name": "length"
                                                    }
                                                }
                                            }
                                        },
                                        "property": {
                                            "type": "Identifier",
                                            "name": "concat"
                                        }
                                    }
                                }
                            },
                            "property": {
                                "type": "Identifier",
                                "name": "length"
                            }
                        }
                    }
                },
                "property": {
                    "type": "Identifier",
                    "name": "length"
                }
            }
        }
    ]
}

我正在寻找身体对象"type"="CallExpression"的所有被调用者。对于解析,我正在使用JSPath库。

为此,我的代码为

var collee= JSPath.apply('.body.callee',parse_tree,{ "type":"CallExpression"})

但它抛出错误 缓存[path]不是函数 ,可能是因为返回值是object类型的?

0 个答案:

没有答案