Angular JS中的嵌套JSON解析

时间:2015-04-27 17:38:59

标签: javascript json angularjs

我的JSON如下。我想使用行中的2ndCol,3rdCol,4thCol。

{  

   "workflows":[  
      {  
         "New":{  
            "All":{  
               "sections":[  
                  {  
                     "id":"section_1",
                     "section":"",
                     "title":"SEPG Audit Checklist",
                     "rows":[  
                        {  
                            "id" : "0",
                            "label" : "How do you establish and maintain the description of the process needs and objectives for the organization?",
                            "2ndCol" : {
                                "type" : "select",
                                "source":[  
                                      {  
                                         "id":"Yes",
                                         "value":"Yes"
                                      },
                                      {  
                                         "id":"No",
                                         "value":"No"
                                      },
                                      {  
                                         "id":"N/A",
                                         "value":"N/A"
                                      }
                                ],
                                "value":[],
                                "required":true,
                                "disabled":false,
                                "hidden":false
                            },
                            "3rdCol" : {
                                "type" : "select",
                                "source":[  
                                      {  
                                         "id":"Yes",
                                         "value":"Yes"
                                      },
                                      {  
                                         "id":"No",
                                         "value":"No"
                                      }

                                ],
                                "value":[],
                                "required":true,
                                "disabled":false,
                                "hidden":false
                            },
                            "4thCol" : {
                                "type" : "textarea",
                                "label": "Comments",
                                "PlaceHolder":"Enter Comment",
                                "Value":""
                            }
                        },
                        {  
                            "id" : "1",
                            "label" : "Explain Organizational process performance objectives?",
                            "2ndCol" : {
                                "type" : "select",
                                "source":[  
                                      {  
                                         "id":"Yes",
                                         "value":"Yes"
                                      },
                                      {  
                                         "id":"No",
                                         "value":"No"
                                      },
                                      {  
                                         "id":"N/A",
                                         "value":"N/A"
                                      }
                                ],
                                "value":[],
                                "required":true,
                                "disabled":false,
                                "hidden":false
                            },
                            "3rdCol" : {
                                "type" : "select",
                                "source":[  
                                      {  
                                         "id":"Yes",
                                         "value":"Yes"
                                      },
                                      {  
                                         "id":"No",
                                         "value":"No"
                                      }

                                ],
                                "value":[],
                                "required":true,
                                "disabled":false,
                                "hidden":false
                            },
                            "4thCol" : {
                                "type" : "textarea",
                                "label": "Comments",
                                "PlaceHolder":"Enter Comment",
                                "Value":""
                            }
                        }
                         ]
                  }
               ]

            }
         }
      }
   ]
}

我可以使用行的id,标签。但是当我试图在行中使用2ndCol,3rdCol和4thCol时。它显示未被捕获的异常非法论证。

我的角度控制器,

angular.forEach($scope.auditJSON.workflows, function(workflow, workflowIndex) {
                    angular.forEach(workflow, function(workflowValue, workflowKey) {
                        angular.forEach(workflowValue, function(value, roleKey) {
                            angular.forEach(value.sections, function(section, sectionIndx) {
                                angular.forEach(section.rows, function(row, rowIndx) {  

                                    console.log(row.label); // It shows fine

                                    console.log(row.2ndCol.type); // It shows the Error

                                });
                            });
                        });
                    });
                });

我无法弄清楚问题。

2 个答案:

答案 0 :(得分:2)

你的write()有点搞砸了。

enter image description here

angular.forEach

我实际上必须创建一个回购,以便您可以看到它是如何工作的。另外,请不要在制作中使用那么多angular.forEach($scope.data, function (workflow, workflowIndex) { angular.forEach(workflow, function (workflowValue, workflowKey) { angular.forEach(workflowValue, function (value, roleKey) { angular.forEach(value, function (sections, sectionIndx) { angular.forEach(sections, function (section, rowIndx) { angular.forEach(section, function (row, rowIndx) { angular.forEach(row, function (row, rowIndx) { if (rowIndx === 'rows') { angular.forEach(row, function (row, rowIndx) { var row2 = row['2ndCol']; var row3 = row['3rdCol']; console.log(row2, row3); }); } }); }); }); }); }); }); });

编辑: https://github.com/eknowles/blank-ng/tree/29902467/nested-json-parsing-in-angular-js

答案 1 :(得分:-1)

你写的是你想获得2ndCol和3rdCol类型的值[你在评论中写下来]

 console.log(row.2ndCol.type); // It shows the Error

你也写了显示错误。 我给出了代码块。这应该工作。

angular.forEach($scope.auditJSON.workflows, function(workflow, workflowIndex) {
                angular.forEach(workflow, function(workflowValue, workflowKey) {
                    angular.forEach(workflowValue, function(value, roleKey) {
                        angular.forEach(value.sections, function(section, sectionIndx) {
                            angular.forEach(section.rows, function(row, rowIndx) {  
                                    if(row['2ndCol'].type === 'select' && row['3rdCol'].type === 'select'){
                                        if(row['2ndCol'].value != row['3rdCol'].value){
                                            console.log(row['2ndCol'].value);
                                            console.log(row['3rdCol'].value);
                                        }
                                    }

                            });
                        });
                    });
                });
            });

如果它解决了您的问题,请接受答案。