javascript - 复杂 JSON 解析问题(angularjs 和 javascript)

标签 javascript json angularjs

我的 json 如下,

{
    "natureitems": [
        {
            "groups": "fruits",
            "items": [
                {
                    "name": "apple",
                    "properties": {
                        "color": "red",
                        "shape": "round"
                    }
                },
                {
                    "name": "orange",
                    "properties": {
                        "color": "orange",
                        "shape": "round"
                    }
                }
            ]
        },
        {
            "groups": "vegitables",
            "items": [
                {
                    "name": "carrot",
                    "properties": {
                        "color": "red",
                        "shape": "cone"
                    }
                },
                {
                    "name": "tomoto",
                    "properties": {
                        "color": "red",
                        "shape": "round"
                    }
                }
            ]
        }
    ]
}

我需要单个li ng-repeat( Angular View )的输出,例如

------------------
fruits(these titles row in different background colors)
------------------
apple   - round
orange  - round

----------------
vegitables
---------------
carrot - cone
tomoto - round

我尝试了很多 for 循环和 Angular foreach,但我分别获取项目和分组。但之后尝试引入上述结构(title-items-title -items)就很难引入一个数组了。

是否有任何简单的方法可以将这种格式的这些项目放入数组或对象中?这样我就可以轻松绑定(bind)到 ng-repeat!

最佳答案

如果没有绑定(bind)单层,则需要使用嵌套的ng-repeat:

<ul>
    <li ng-repeat="group in object.natureitems">
        <span class="group-title">{{group.groups}}</span>
        <ul>
            <li ng-repeat="item in group.items">{{item.name+" - "+"item.properties.shape"}}</li>
        </ul>
    </li>
</ul>

这是一个例子plunker

关于javascript - 复杂 JSON 解析问题(angularjs 和 javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29326511/

相关文章:

javascript - 无法在 Node js 教程中提交表单

java - 如何让 JAX-RS 返回 Java 8 LocalDateTime 属性作为 JavaScript 样式的日期字符串?

json - Loganalytics 工作区 ID 参数模板

json - jq:将对象数组中的字段提升到父对象中

angularjs - 将消息格式与 Angular 翻译结合使用时的变量替换

javascript - 如何从输入字段获取值并写入json文件?

javascript - UI5 - 过滤器组合框放置问题

javascript - 使用 JAM 根据先前的选择值更改选择选项

javascript - 仅将 AngularJS 用于路由目的

javascript - AngularJS,如何从 cookie 获取域