2015-12-21 13 views
1

我的JSON是這個

[ 
{ 
    "usuario": { 
     "id": 2, 
     "username": "licorera1" 
    }, 
    "productos": [ 
     { 
      "id": 2, 
      "descripcion": "Botella", 
      "precio": "1500000", 
      "marca": { 
       "nombre": "Red Label", 
       "tipo": { 
        "descripcion": "Whiskey" 
       } 
      }, 
      "imagen": "http://192.168.0.12:8000/media/producto/redlabel.png" 
     }, 
     { 
      "id": 1, 
      "descripcion": "Media Botella", 
      "precio": "50000", 
      "marca": { 
       "nombre": "Nectar", 
       "tipo": { 
        "descripcion": "Aguardiente" 
       } 
      }, 
      "imagen": "http://192.168.0.12:8000/media/producto/nectarrojo_qgCTYsx.jpg" 
     } 
    ] 
} 
] 

和我想做的事是讓只有德對象「PRODUCTOS」。

的JSON的整個網址是http://192.168.0.12:8000/api/productos_licorera/?pk=2&licorera=

所以在我services.js我使用$資源得到德JSON

這是我services.js

(function(){ 

angular.module('productos.services',['ngResource']) 

.factory('LicoresService',['$resource',function($resource){ 
return $resource('http://192.168.0.12:8000/api/productos_licorera/', {}, { 
    query: {method:'GET', params:{pk:2,licorera:''}, isArray:true} 
    }); 
}]); 
})(); 

這是我的controller.js

(function(){ 

angular.module('productos.controllers',[]) 
.controller('LicoresController',['$scope','$http', '$resource', 'LicoresService',function($scope, $http,$resource, LicoresService){ 
    var licores; 
    var show = false; 
    $scope.licores = LicoresService.query(); 
}]) 
.controller('TabsController', function() { 
    this.tab = 4; 
    this.selectTab = function (tab) { 
    this.tab = tab; 
     console.log(this.tab); 
    }; 
});  
})(); 

我需要在變量「licores」n所有的Json,只有「productos」對象。

回答

0

JSON是'傳輸'格式。您無法傳輸部分JSON。
雖然,你已經解析JSON後,常規的JavaScript對象,你可以選擇的關鍵你喜歡:

$scope.licores = JSON.parse(LicoresService.query())[0].licores; 

希望這可以幫助你......

+0

感謝您的幫助,但它不作品。我正在考慮get({id:'1'})之類的東西,但它可以與我需要的密鑰一起使用,例如get({product})。有這樣的事情嗎? –