如何解析json数组并获得预期结果?

用户名

我正在解析json。我正在检查json数组,然后创建另一个对象。我只是碰到一个地方。实际上,我正在检查父对象是否有孩子,我在“ testCaseList”数组中添加了一个针对孩子的对象。但是我需要检查子ID是否具有字符“ not”,应在此数组“ commandList”中添加http://jsfiddle.net/tJ7Kq/5/这是我的输入。

[
  {
    "id": "a",
    "text": "a",
    "icon": true,
    "li_attr": {
      "id": "a"
    },
    "a_attr": {
      "href": "#"
    },
    "state": {
      "loaded": true,
      "opened": false,
      "selected": false,
      "disabled": false
    },
    "data": {

    },
    "children": [

    ]
  },
  {
    "id": "b",
    "text": "b\n            ",
    "icon": true,
    "li_attr": {
      "id": "b"
    },
    "a_attr": {
      "href": "#"
    },
    "state": {
      "loaded": true,
      "opened": false,
      "selected": false,
      "disabled": false
    },
    "data": {

    },
    "children": [
      {
        "id": "b-a-1",
        "text": "b-a",
        "icon": true,
        "li_attr": {
          "id": "b-a-1"
        },
        "a_attr": {
          "href": "#"
        },
        "state": {
          "loaded": true,
          "opened": false,
          "selected": false,
          "disabled": false
        },
        "data": {

        },
        "children": [

        ]
      },
      {
        "id": "b-b-2",
        "text": "b-b\n                    ",
        "icon": true,
        "li_attr": {
          "id": "b-b-2"
        },
        "a_attr": {
          "href": "#"
        },
        "state": {
          "loaded": true,
          "opened": false,
          "selected": false,
          "disabled": false
        },
        "data": {

        },
        "children": [
          {
            "id": "b-b-a",
            "text": "b-b-a",
            "icon": true,
            "li_attr": {
              "id": "b-b-a"
            },
            "a_attr": {
              "href": "#"
            },
            "state": {
              "loaded": true,
              "opened": false,
              "selected": false,
              "disabled": false
            },
            "data": {

            },
            "children": [

            ]
          },
          {
            "id": "b-b-b",
            "text": "b-b-b",
            "icon": true,
            "li_attr": {
              "id": "b-b-b"
            },
            "a_attr": {
              "href": "#"
            },
            "state": {
              "loaded": true,
              "opened": false,
              "selected": false,
              "disabled": false
            },
            "data": {

            },
            "children": [

            ]
          }
        ]
      }
    ]
  },
  {
    "id": "c-1",
    "text": "c\n            ",
    "icon": true,
    "li_attr": {
      "id": "c-1"
    },
    "a_attr": {
      "href": "#"
    },
    "state": {
      "loaded": true,
      "opened": false,
      "selected": false,
      "disabled": false
    },
    "data": {

    },
    "children": [
      {
        "id": "not-c-a-1",
        "text": "c-a",
        "icon": true,
        "li_attr": {
          "id": "not-c-a-1"
        },
        "a_attr": {
          "href": "#"
        },
        "state": {
          "loaded": true,
          "opened": false,
          "selected": false,
          "disabled": false
        },
        "data": {

        },
        "children": [

        ]
      },
      {
        "id": "not-c-b-2",
        "text": "b-b",
        "icon": true,
        "li_attr": {
          "id": "not-c-b-2"
        },
        "a_attr": {
          "href": "#"
        },
        "state": {
          "loaded": true,
          "opened": false,
          "selected": false,
          "disabled": false
        },
        "data": {

        },
        "children": [

        ]
      }
    ]
  }
]

出去把这个

[
  {
    "a": {
      "commandList": [],
      "testCaseList": []
    }
  },
  {
    "b": {
      "commandList": [],
      "testCaseList": [
        {
          "b-a-1": {
            "commandList": [],
            "testCaseList": []
          }
        },
        {
          "b-b-2": {
            "commandList": [],
            "testCaseList": [
              {
                "b-b-a": {
                  "commandList": [],
                  "testCaseList": []
                }
              },
              {
                "b-b-b": {
                  "commandList": [],
                  "testCaseList": []
                }
              }
            ]
          }
        }
      ]
    }
  },
  {
    "c-1": {
      "commandList": [],
      "testCaseList": [
        {
          "not-c-a-1": {
            "commandList": [],
            "testCaseList": []
          }
        },
        {
          "not-c-b-2": {
            "commandList": [],
            "testCaseList": []
          }
        }
      ]
    }
  }
]

预期输出为:[

      {
        "a": {
          "commandList": [],
          "testCaseList": []
        }
      },
      {
        "b": {
          "commandList": [],
          "testCaseList": [
            {
              "b-a-1": {
                "commandList": [],
                "testCaseList": []
              }
            },
            {
              "b-b-2": {
                "commandList": [],
                "testCaseList": [
                  {
                    "b-b-a": {
                      "commandList": [],
                      "testCaseList": []
                    }
                  },
                  {
                    "b-b-b": {
                      "commandList": [],
                      "testCaseList": []
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "c-1": {
          "commandList": [
 {
              "not-c-a-1": {
                "commandList": [],
                "testCaseList": []
              }
            },
            {
              "not-c-b-2": {
                "commandList": [],
                "testCaseList": []
              }
            }],
          "testCaseList": []
        }
      }
    ]
登高

这个mapItem函数将满足您的需求:

function mapItem(inputItem) {
    var item = {};
    item[inputItem.id] = JSON.parse(sessionStorage.getItem(inputItem.id));

    for (k in inputItem.children) {
        if (/^not-/.test(inputItem.children[k].id)) {
            item[inputItem.id].commandList.push(mapItem(inputItem.children[k]));
        }else{
            item[inputItem.id].testCaseList.push(mapItem(inputItem.children[k]));  
        }
    }

    return item;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何解析json数组并获得预期结果?

来自分类Dev

如何解析XML以获得所需的结果

来自分类Dev

如何解析XML以获得所需的结果

来自分类Dev

如何解析JSON数组

来自分类Dev

如何解析此XML数据并获得准确的结果?

来自分类Dev

如何解析对象数组的JSON结果并在Excel中打印?

来自分类Dev

如何解析这种json数组

来自分类Dev

如何解析内部数组json

来自分类Dev

如何解析JSON多维数组

来自分类Dev

如何解析数组内的Json对象?

来自分类Dev

如何解析特定的Json数组

来自分类Dev

如何解析JSON数组(Language array)?

来自分类Dev

如何解析这样的json数组?

来自分类Dev

如何解析嵌套的JSON数组?

来自分类Dev

如何解析这个json数组

来自分类Dev

如何解析 Json 数组对象?

来自分类Dev

Android:如何解析对象数组的JSON数组

来自分类Dev

如何解析JSON对象数组并从中创建数组

来自分类Dev

如何解析具有多个行/结果的JSON

来自分类Dev

如何解析和显示json数组中的url内容?

来自分类Dev

如何解析JSON动态数组名称

来自分类Dev

如何解析JSON文档并提取数组?

来自分类Dev

如何解析字符串数组的JSON

来自分类Dev

如何解析具有多个数组的JSON?

来自分类Dev

如何解析具有值“ id”的Json数组:1.1.1.1

来自分类Dev

如何解析JSON数组以更新PostgreSQL中的记录

来自分类Dev

具有值数组的json键-如何解析

来自分类Dev

如何解析 JSON 格式的复选框多维数组?

来自分类Dev

如何解析 node.js 中的 json 数组列表?