waynev waynev - 1 year ago 131
AngularJS Question

Parse JSON array to object

I need to parse my JSON array to object in Angular, If I am correct but got no idea how to do that. I have read a few posts, and there many on the subject but for some reason I can not get it right.

Link to my JSON: http://wingfield.vmgdemo.co.za/webapi/view_stock_complete


app.controller('showRoom', function($scope, $http) {
then(function(response) {
$scope.view_stock_complete = response.data;


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

Answer Source

Your data is an array, so you'll need to loop over them using ng-repeat, or access the first entry using view_stock_complete[0]

  'use strict';
  angular.module('test', [])
    .controller('TestController', TestController);
  TestController.$inject = ['$http'];
  function TestController($http) {
    var vm = this;
    vm.data = [];
      vm.data = response.data;
<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}}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download