在角度控制器中迭代嵌套的json数组并获取唯一值并在视图html页面中显示

时间:2016-07-28 04:20:40

标签: json

这是我的json我发现很难提取所需的相应数组的值。

如何显示日程表数组中的静态值并在html页面中显示它们。 我用foreach循环和对象概念尝试了很多。

JSON:

$ scope.sp = {“basicInfo”:{     “firstName”:“Nishad”,     “lastName”:“Janan”,     “性别”:“男性”,     “dob”:“22-04-1993”,     “mobile”:“9744444883”,     “email”:“nandnonly@gmail.com”,     “availableFrom”:“01-01-2016”,     “availableTo”:“31”,     “enableOnlineApmt”: “真”,     “enableLogin”:“禁用”     },

"additionalInfo": {
"qualification": "Plus Two",
"specialization": "Commerce",
"achievements": "None",
"address": "Kumbakonam",
"languages": ["malayalam", "english"],
"facebookUrl": "www.facebook.com",
"linkedInUrl": "www.linkedin.com",
"twitterUrl": "www.twitter.com",
"webPageUrl": "www.google.com"
},
"assignedservices": [2, 3, 5, 6],
"schedlueWithServices":[{
"schedule": {
"timespec": [{
"recurringType": "Weekly",
"repeatIntervals": ["1", "7"],
"startDate": "2016-07-08",
"terminator": {
"endDate": "2017-07-08",
"noOfOccurance": ""
},
"timeSlots": [{
"sTime": "09:00:AM",
"eTime": "02:00:PM"
}]
}]
},
"services": [2, 3],
"schType": "Business",
"serviceChange": "false",
"isScheduleChange": "false"

},{
"schedule": {
"timespec": [{
"recurringType": "Weekly",
"repeatIntervals": ["1", "7"],
"startDate": "2016-07-08",
"terminator": {
"endDate": "2017-07-08",
"noOfOccurance": ""
},
"timeSlots": [{
"sTime": "09:00:AM",
"eTime": "02:00:PM"
}]
}]
},
"services": [5, 6],
"schType": "Business",
"serviceChange": "false",
"isScheduleChange": "false"

}],
"isAdmin": "true"
}

我创建了一个函数来显示来自timespec的值,但代码中出现了错误。我无法修复代码。如果有人可以帮助它。

function schedule($scope, $http) {
  $http({
    method: 'GET',
      }).success(function(data) {
    $scope.schedlueWithServices = data.schedlueWithServices; 
    $scope.schedule = [];
    $scope.timespec=[];
    angular.forEach(data.schedlueWithServices, function(schedlueWithServices, index) {
      angular.forEach(schedlueWithServices.schedule, function(schedule, index){
        angular.forEach(schedule.timespec, function(timespec, index){
            $scope.timespec.push(timespec);
        });
      });
    });
  });
}

1 个答案:

答案 0 :(得分:0)

很难准确理解您尝试显示/解析的值,但它应该只是将括号表示法与点表示法结合起来。

var obj = {
    "additionalInfo": {
"qualification": "Plus Two",
"specialization": "Commerce",
"achievements": "None",
"address": "Kumbakonam",
"languages": ["malayalam", "english"],
"facebookUrl": "www.facebook.com",
"linkedInUrl": "www.linkedin.com",
"twitterUrl": "www.twitter.com",
"webPageUrl": "www.google.com"
},
"assignedservices": [2, 3, 5, 6],
"schedlueWithServices":[{
"schedule": {
"timespec": [{
"recurringType": "Weekly",
"repeatIntervals": ["1", "7"],
"startDate": "2016-07-08",
"terminator": {
"endDate": "2017-07-08",
"noOfOccurance": ""
},
"timeSlots": [{
"sTime": "09:00:AM",
"eTime": "02:00:PM"
}]
}]
},
"services": [2, 3],
"schType": "Business",
"serviceChange": "false",
"isScheduleChange": "false"

},{
"schedule": {
"timespec": [{
"recurringType": "Weekly",
"repeatIntervals": ["1", "7"],
"startDate": "2016-07-08",
"terminator": {
"endDate": "2017-07-08",
"noOfOccurance": ""
},
"timeSlots": [{
"sTime": "09:00:AM",
"eTime": "02:00:PM"
}]
}]
},
"services": [5, 6],
"schType": "Business",
"serviceChange": "false",
"isScheduleChange": "false"

}],
"isAdmin": "true"
} 
;

  console.log(obj.schedlueWithServices[1].schedule.timespec[0].recurringType);
console.log(obj.schedlueWithServices[1].schedule.timespec[0].repeatIntervals[0]);
console.log(obj.schedlueWithServices[1].schedule.timespec[0].repeatIntervals[1]);
console.log(obj.schedlueWithServices[1].schedule.timespec[0].timeSlots[0].sTime);
console.log(obj.schedlueWithServices[1].schedule.timespec[0].timeSlots[0].eTime);

我使用console.log来证明如何获取请求的静态值。您需要做的就是将它们分配给变量值并将该值放在$scope