2016-11-22 89 views
2

的我是相当新的JSON,我试图提取名为“步”的阵列并在此JSON字符串“步”获得angularJS请求结果:如何获得JSON数组out对象

Payload

payload.data.steps 
payload.data.steps[0].step 
payload.data[0].steps 

任何GUI:

Payload.data

但是,我不能尝试以下(这给未定义或错误)得到这个信息舞蹈将不胜感激,谢谢!

已解答 谢谢大家,您的深思熟虑的答案。我犯了一些错误,包括具有异步http.get结果的错误。

以供将来参考,在我services.js我通过GET请求来我的控制器是这样的:

var recipes = payload.data[0].steps; 

,在我controller.js我确信有它等待调用回来(而不是接受一个承诺),然后将其保存到范围:

RecipeDetails.getInstructions($scope.details.id).then(function(InstructionPayload){ 
     $scope.instructions = InstructionPayload; 

,在我的HTML页面中显示单独的步骤:

//Steps: 
<div ng-repeat="list in instructions track by $index"> 
<p>{{ list.step }}</p> 
+0

'payload.data [0] .steps'应该管用。也许你可以在pastebin.com上托管源代码json,这样我可以进一步分析了吗? –

+0

你可以用'fiddle'或'plnkr'分享代码吗? –

+0

payload.data [0] .steps在我在services.js文件中而不是在控制器中工作!谢谢。 – parynaz

回答

0

这里快速浏览一下,希望它有帮助。我可以随时更新,如果需要。

function exampleController($scope) { 
 
    $scope.example = [{ 
 
    name: "", 
 
    steps: [{ 
 
     equipment: [], 
 
     ingredients: [], 
 
     number: 1, 
 
     step: 'Blah' 
 
    }] 
 
    }, { 
 
    name: "", 
 
    steps: [{ 
 
     equipment: [], 
 
     ingredients: [], 
 
     number: 2, 
 
     step: 'Bibbidi' 
 
    }] 
 
    }, { 
 
    name: "", 
 
    steps: [{ 
 
     equipment: [], 
 
     ingredients: [], 
 
     number: 3, 
 
     step: 'Bobbidi' 
 
    }] 
 
    }, { 
 
    name: "", 
 
    steps: [{ 
 
     equipment: [], 
 
     ingredients: [], 
 
     number: 4, 
 
     step: 'Boo' 
 
    }] 
 
    }]; 
 

 
    function extractSteps() { 
 
    $scope.stepArray = []; 
 
    $scope.example.forEach(function(step) { 
 
     if (step.steps && Array.isArray(step.steps)) { 
 
     step.steps.forEach(function(value, index) { 
 
      if (value && value.step) { 
 
      $scope.stepArray.push({ 
 
       'step': value.step 
 
      }) 
 
      } 
 
     }); 
 
     } 
 
    }); 
 
    } 
 
    extractSteps(); 
 
} 
 

 
angular 
 
    .module('app', []) 
 
    .controller('exampleController', exampleController);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container-fluid" ng-app="app"> 
 
    <div class="container" ng-controller="exampleController"> 
 
    <div class="row" ng-repeat="(key, value) in example" ng-bind="value.steps[0].step"> 
 
    </div> 
 
    <div class="row" ng-repeat="value in stepArray"> 
 
     <pre>{{value | json}}</pre> 
 
    </div> 
 
    </div> 
 
</div>

0

如果你在你的应用程序,你可以链下划线或lodash一些东西放在一起,就像这样:

_.chain(obj.data) // wrap the data so we can chain transformations 
    .map('steps') // gather all the steps arrays from the main array 
    .flatten() // smash them into a single array 
    .map('step') // grab all the step properties from those 
    .value() // return the value -> ["Blah", "Bibbidi", "Bobbidi", "Boo"] from alphapilgrim's example data