使用键

时间:2017-01-04 07:48:16

标签: java json spring-boot

我正在使用弹簧启动角度js进行应用。在这个我正在处理json数组和json对象使用abn-tree构造树。

树构建精细。由于此树将在单击每个节点时更新,意味着将在运行时添加子项,因为单击时从数据库加载。

Tree JSON就像:

 [{"parent":"Title-1","expanded":true,"label":"Title-1","children":[
    {"parent":"Title-1","expanded":true,"label":"Chapter-I", "children":[
        {"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-1"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-10"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-100"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-11"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-12"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-13"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-14"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-140"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-49"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-5"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-50"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-7"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-75"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-8"},{"parent":"Chapter-I","expanded":false,"children":[],"label":"Part-9"}]},
    {"parent":"Title-1","expanded":true,"label":"Chapter-II","children":[
        {"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-200"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-201"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-202"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-203"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-204"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-205"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-209"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-210"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-211"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-285"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-286"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-287"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-288"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-289"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-290"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-300"},{"parent":"Chapter-II","expanded":false,"children":[],"label":"Part-301"}]}]
}]

上面返回到UI和树显示,所以我在后端资源类中构建这个json以避免js文件中的代码。

随着每个部分将添加子部分/子组等,这个json将增长更多...下一个子部分将添加他们的子部分。所以part.subpart ..的子项将添加对象来加载他们的孩子。

随着它越来越大,遍历这个json的时间越来越长。循环也在增加。是否有任何简单的方法来获取给定字符串的jsonarray以在其上添加更多子元素而不进行循环。

就像我想要Part-200的孩子一样,我只提供节点名称并获得Part-200的json数组而不进行循环和迭代?

1 个答案:

答案 0 :(得分:0)

请尽可能简化JSON数据。当您从API获得响应时可能会很大。编写一些函数来简化JSON并在进入视图级别之前将它们存储在范围变量中。像ng-repeat这样的循环会很复杂。我们每次都不想去loops。因此,请确保要在视图中显示的所有必需数据都在范围变量中,以便我们可以从视图中轻松访问它们

相关问题