将json字符串对象转换为json Array

时间:2015-06-04 09:11:35

标签: javascript jquery json

  • 我需要格式化我的json字符串,使所有名称为“children”的节点都应该是数组类型。
  • 在某些地方,我已经将孩子作为阵列类型,但并非所有地方都没有。
  • 如果它已经是一个数组类型,则无需对其进行任何操作。

输入:

 "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": ""
                                    }
                                },

预期产出:

 "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": []
                                    }]
                                },

0 个答案:

没有答案