将JSON数组解析为object

时间:2016-11-08 16:09:49

标签: javascript angularjs json

我需要将我的JSON数组解析为Angular中的对象,如果我是正确的但不知道如何做到这一点。我已经阅读了一些帖子,有很多关于这个主题但由于某些原因我无法做到正确。

链接到我的JSON:http://wingfield.vmgdemo.co.za/webapi/view_stock_complete

// JS

app.controller('showRoom', function($scope, $http) {
$http.get('http://wingfield.vmgdemo.co.za/webapi/view_stock_complete').
    then(function(response) {
        $scope.view_stock_complete = response.data;
    });
});

// HTML

 <div class="container" ng-controller="showRoom">
<div><span>Variant: {{view_stock_complete.stock_id}}</span></div>
 </div>

1 个答案:

答案 0 :(得分:0)

您的数据是一个数组,因此您需要使用ng-repeat对其进行循环,或使用view_stock_complete[0]访问第一个条目

&#13;
&#13;
(function(){
  'use strict';
  
  angular.module('test', [])
    .controller('TestController', TestController);
  
  TestController.$inject = ['$http'];
  
  function TestController($http) {
    var vm = this;
    
    vm.data = [];
    $http.get('http://wingfield.vmgdemo.co.za/webapi/view_stock_complete').then(function(response){
      vm.data = response.data;
    })
  }
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

<div ng-app='test' ng-controller='TestController as tc'>
  <div ng-show="tc.data.length == 0">loading...</div>
  <div ng-repeat="car in tc.data">
    {{$index + 1}}. {{car.make}} {{car.series}}
  </div>
</div>
&#13;
&#13;
&#13;