如何使用Angular解析嵌套的JSON并动态获取所需的字段?

时间:2015-11-12 15:55:13

标签: javascript json angularjs

我生成了一个JSON,我需要在json中访问各种字段,例如message-name。我无法迭代并从json中获取数据。

"playlist": [
      {
        "pick-policy": "sequence",
        "item-list": {
          "script": [
            {
              "max-expected-duration": "00:00:00.00",
              "message-identifier": "50160",
              "message-name": "Visa - WF - Denver - Module - Campus Buildings",
              "ingredients": {
                "text": [
                  {
                    "_name": "filter"
                  },
                  {
                    "_name": "title",
                    "__text": "Campus Buildings"
                  },
                  {
                    "_name": "feature",
                    "__text": "map"
                  }
                ],
                "resource": {
                  "_name": "itemPlaylist",
                  "__text": "Visa - WF - Denver - Buildings"
                },
                "filename": {
                  "_name": "icon",
                  "__text": "Signet Holding/visa/icons/campus[50047518500].png"
                }
              },
              "metadata": {
                "string": [
                  {
                    "_name": "MediaItem.ModifiedDate",
                    "__text": "2015-11-10T14:51:24-0600"
                  },
                  {
                    "_name": "MediaItem.OwnerWorkgroup",
                    "__text": "Denver"
                  },
                  {
                    "_name": "MediaItem.keywords"
                  },
                  {
                    "_name": "MediaItem.zip code"
                  },
                  {
                    "_name": "MediaItem.LocationType"
                  },
                  {
                    "_name": "MediaItem.OverrideName"
                  },
                  {
                    "_name": "MediaItem.Industry"
                  },
                  {
                    "_name": "MediaItem.SlideCount"
                  }
                ]
              },
              "_name": "apps/WayFinding_Module[50014673103].scb"
            },
            {
              "max-expected-duration": "00:00:00.00",
              "message-identifier": "50161",
              "message-name": "Visa - WF - Denver - Module - Conference",
              "ingredients": {
                "text": [
                  {
                    "_name": "title",
                    "__text": "Conference Rooms"
                  },
                  {
                    "_name": "filter",
                    "__text": "Conference"
                  },
                  {
                    "_name": "feature",
                    "__text": "list"
                  }
                ],
                "filename": {
                  "_name": "icon",
                  "__text": "Signet Holding/visa/icons/conference[50047520000].png"
                }
              },
              "metadata": {
                "string": [
                  {
                    "_name": "MediaItem.ModifiedDate",
                    "__text": "2015-11-10T14:50:57-0600"
                  },
                  {
                    "_name": "MediaItem.OwnerWorkgroup",
                    "__text": "Denver"
                  },
                  {
                    "_name": "MediaItem.keywords"
                  },
                  {
                    "_name": "MediaItem.zip code"
                  },
                  {
                    "_name": "MediaItem.LocationType"
                  },
                  {
                    "_name": "MediaItem.OverrideName"
                  },
                  {
                    "_name": "MediaItem.Industry"
                  },
                  {
                    "_name": "MediaItem.SlideCount"
                  }
                ]
              },
              "_name": "apps/WayFinding_Module[50014673103].scb"
            },
            {
              "max-expected-duration": "00:00:00.00",
              "message-identifier": "50159",
              "message-name": "Visa - WF - Denver - Module - Hotelling",
              "ingredients": {
                "text": [
                  {
                    "_name": "filter",
                    "__text": "Hotelling"
                  },
                  {
                    "_name": "title",
                    "__text": "Hotelling Areas"
                  },
                  {
                    "_name": "feature",
                    "__text": "list"
                  }
                ],
                "filename": {
                  "_name": "icon",
                  "__text": "Signet Holding/visa/icons/hotel[50047521600].png"
                }
              },
              "metadata": {
                "string": [
                  {
                    "_name": "MediaItem.ModifiedDate",
                    "__text": "2015-11-10T14:51:39-0600"
                  },
                  {
                    "_name": "MediaItem.OwnerWorkgroup",
                    "__text": "Denver"
                  },
                  {
                    "_name": "MediaItem.keywords"
                  },
                  {
                    "_name": "MediaItem.zip code"
                  },
                  {
                    "_name": "MediaItem.LocationType"
                  },
                  {
                    "_name": "MediaItem.OverrideName"
                  },
                  {
                    "_name": "MediaItem.Industry"
                  },
                  {
                    "_name": "MediaItem.SlideCount"
                  }
                ]
              },
              "_name": "apps/WayFinding_Module[50014673103].scb"
            },
            {
              "max-expected-duration": "00:00:00.00",
              "message-identifier": "50162",
              "message-name": "Visa - WF - Denver - Module - Huddle",
              "ingredients": {
                "text": [
                  {
                    "_name": "feature",
                    "__text": "list"
                  },
                  {
                    "_name": "filter",
                    "__text": "Huddle"
                  },
                  {
                    "_name": "title",
                    "__text": "Huddle Rooms"
                  }
                ],
                "filename": {
                  "_name": "icon",
                  "__text": "Signet Holding/visa/icons/conference[50047520000].png"
                }
              },
              "metadata": {
                "string": [
                  {
                    "_name": "MediaItem.ModifiedDate",
                    "__text": "2015-11-10T14:51:11-0600"
                  },
                  {
                    "_name": "MediaItem.OwnerWorkgroup",
                    "__text": "Denver"
                  },
                  {
                    "_name": "MediaItem.keywords"
                  },
                  {
                    "_name": "MediaItem.zip code"
                  },
                  {
                    "_name": "MediaItem.LocationType"
                  },
                  {
                    "_name": "MediaItem.OverrideName"
                  },
                  {
                    "_name": "MediaItem.Industry"
                  },
                  {
                    "_name": "MediaItem.SlideCount"
                  }
                ]
              },
              "_name": "apps/WayFinding_Module[50014673103].scb"
            },
            {
              "max-expected-duration": "00:00:00.00",
              "message-identifier": "50158",
              "message-name": "Visa - WF - Denver - Module - Shuttle",
              "ingredients": {
                "text": [
                  {
                    "_name": "title",
                    "__text": "Shuttle Schedule"
                  },
                  {
                    "_name": "feature",
                    "__text": "shuttle"
                  },
                  {
                    "_name": "filter"
                  }
                ],
                "filename": {
                  "_name": "icon",
                  "__text": "Signet Holding/visa/icons/shuttle[50047524600].png"
                }
              },
              "metadata": {
                "string": [
                  {
                    "_name": "MediaItem.ModifiedDate",
                    "__text": "2015-11-10T14:51:54-0600"
                  },
                  {
                    "_name": "MediaItem.OwnerWorkgroup",
                    "__text": "Denver"
                  },
                  {
                    "_name": "MediaItem.keywords"
                  },
                  {
                    "_name": "MediaItem.zip code"
                  },
                  {
                    "_name": "MediaItem.LocationType"
                  },
                  {
                    "_name": "MediaItem.OverrideName"
                  },
                  {
                    "_name": "MediaItem.Industry"
                  },
                  {
                    "_name": "MediaItem.SlideCount"
                  }
                ]
              },

我没有使用角度为它创建工厂。但在那之后我迷失了:

var jsonPlan = {'modules':[]};

//  console.log(playlists);
    angular.forEach(playlists, function(playlist, key) {
//      console.log(playlist._name);
        if(playlist._name == modulePlaylistName){
            angular.forEach(playlist['item-list'].content, function(module, key2) {
                var tempModule = {};
                angular.forEach(module.ingredients, function(ingredient, key3) {
                    if(key3 == "resource"){
                        angular.forEach(playlists, function(playlist, key4) {
                            if(playlist._name == ingredient.__text){

                                console.log("found item 'Buildings' playlist");
                                console.log("need to go further down the rabbit hole.......");


                            } 
                        });
                    } else if(ingredient._name === undefined){
                        angular.forEach(ingredient, function(ingredientSub, key4) {
                            tempModule[ingredientSub._name] = ingredientSub.__text;
                        });
                    } else {
                        tempModule[ingredient._name] = ingredient.__text;
                    }
                });

                jsonPlan['modules'].push(tempModule);

            });
        }
    });

请帮助您动态地从中获取特定数据。

0 个答案:

没有答案