使用特定对象节点名称

时间:2015-06-04 06:01:32

标签: javascript jquery json node.js

我有一个像这样的json字符串。它是我的json字符串的一部分。

                                "foldChildren": "false",
                                "branchColor": "#000000",
                                "children": [
                                    {
                                        "id": "3f67cbab-ff5a-4f61-b1ef-d551271e8af2",
                                        "parentId": "211c838e-3e2d-4919-ab09-dd7f8a901b97",
                                        "text": {
                                            "caption": "f",
                                            "font": {
                                                "style": "normal",
                                                "weight": "normal",
                                                "decoration": "none",
                                                "size": "15",
                                                "color": "#000000"
                                            }
                                        },
                                        "offset": {
                                            "x": "30",
                                            "y": "93"
                                        },
                                        "foldChildren": "false",
                                        "branchColor": "#000000",
                                        "children": {
                                            "id": "2ecaecb6-d55c-4203-bace-f14e276700af",
                                            "parentId": "3f67cbab-ff5a-4f61-b1ef-d551271e8af2",
                                            "text": {
                                                "caption": "x",
                                                "font": {
                                                    "style": "normal",
                                                    "weight": "normal",
                                                    "decoration": "none",
                                                    "size": "15",
                                                    "color": "#000000"
                                                }
                                            },
                                            "offset": {
                                                "x": "122",
                                                "y": "114"
                                            },
                                            "foldChildren": "false",
                                            "branchColor": "#000000",
                                            "children": ""
                                        }
                                    },
  • 我需要的是找到名为" children"的第一个节点对象。并获取该子对象内的所有字符串。

    第一步的预期输出

    [
                                {
                                    "id": "3f67cbab-ff5a-4f61-b1ef-d551271e8af2",
                                    "parentId": "211c838e-3e2d-4919-ab09-dd7f8a901b97",
                                    "text": {
                                        "caption": "f",
                                        "font": {
                                            "style": "normal",
                                            "weight": "normal",
                                            "decoration": "none",
                                            "size": "15",
                                            "color": "#000000"
                                        }
                                    },
                                    "offset": {
                                        "x": "30",
                                        "y": "93"
                                    },
                                    "foldChildren": "false",
                                    "branchColor": "#000000",
                                    "children": {
                                        "id": "2ecaecb6-d55c-4203-bace-f14e276700af",
                                        "parentId": "3f67cbab-ff5a-4f61-b1ef-d551271e8af2",
                                        "text": {
                                            "caption": "x",
                                            "font": {
                                                "style": "normal",
                                                "weight": "normal",
                                                "decoration": "none",
                                                "size": "15",
                                                "color": "#000000"
                                            }
                                        },
                                        "offset": {
                                            "x": "122",
                                            "y": "114"
                                        },
                                        "foldChildren": "false",
                                        "branchColor": "#000000",
                                        "children": ""
                                    }
                                },
    
  • 现在我需要获取名称为#34; children"的下一个节点对象。从第一步得到的json字符串。 第二步后的预期输出

      {
                                "id": "2ecaecb6-d55c-4203-bace-f14e276700af",
                                "parentId": "3f67cbab-ff5a-4f61-b1ef-d551271e8af2",
                                "text": {
                                    "caption": "x",
                                    "font": {
                                        "style": "normal",
                                        "weight": "normal",
                                        "decoration": "none",
                                        "size": "15",
                                        "color": "#000000"
                                    }
                                },
                                "offset": {
                                    "x": "122",
                                    "y": "114"
                                },
                                "foldChildren": "false",
                                "branchColor": "#000000",
                                "children": ""
                            }
                        },
    
  • 等等,直到字符串结束。

1 个答案:

答案 0 :(得分:0)

听起来你想要的是在任何给定的JSON结构上找到这个“children”节点,在这种情况下你可以做一个depth-first search来遍历你的整个JSON结构。

查看其他StackOverflow帖子:How to do a recursive search of this Javascript/JSON tree?