【问题标题】:Get a json array inside another json array using key使用 key 在另一个 json 数组中获取一个 json 数组
【发布时间】:2017-05-18 10:35:42
【问题描述】:

我正在使用 Spring Boot Angular js 开发应用程序。在这里,我正在处理 json 数组和 json 对象以使用 abn-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 数组,而不需要循环和迭代?

【问题讨论】:

    标签: java json spring-boot


    【解决方案1】:

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

    【讨论】:

    • 完整的 json 将返回 UI 并存储在范围变量中。我在 java 端遇到问题,不需要对 ui 做任何事情,即 java script angular 端,它只需要显示从资源 java 类返回的数据。我想要一种方便的方式来获取节点,以便我可以用它的子节点更新它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    相关资源
    最近更新 更多