2014-06-21 3 views
0

내 JSON 파일 형식으로되어 있습니다구문 분석 JSON 사용 AngularJS와

{ 
    "doc": { 
    "BandDatabase": { 
     "Artist1": { 
     "-name": "john", 
     "-instrument": "piano" 
     "Artist2": [ 
      { 
      "-name": "tina", 
      "-instrument": "drums" 
      "Manager": { 
       "Person1": { 
       "-name": "abby" 
       } 

내가 아니라 내 컨트롤러 설정이 있습니다

myApp.controller('MyController', ['$scope', '$http', function ($scope, $http) { 

    $http.get('js/artists.json').success(function(data) { 
     $scope.artists = data; 
    }); 

을 어떻게 위의 JSON 형식의 요소를 검색 할 수 있습니까? 에 사용 JavaScript

<div ng-controller = "MyController"> 
    <ul class="artists"> 
     <li class="messages" ng-repeat="item in artists"> 
      <div class="info"> 
       <h1>{{item.name}}</h1> 
      </div> 

내가 Artist1의 이름과 아티스트 2

+0

, Artist1의 속성이 Artist2입니다 :

$http.get('js/artists.json').success(function(data) { $scope.artists = JSON.Parse(data).artists; }); 

은 BTW 당신의 JSON 나는 더 같이한다 상상 ... 나에게 오히려 이상한 보인다? Artist1은 객체이고 Artist2는 배열입니까? – Martin

답변

1

JSON.Parse를 표시 할 수 있도록하려는되는 부분 : 나는 웹 페이지에 이름 요소를 표시 할 수 없습니다 나는 내 다음을 수행 JSONJavaScript Objects으로 변환하십시오.

하여 JSON 파일에서
{ 
    "artists": [ 
     { 
      "name": "kongor", 
      "instrument": "gitar" 
     }, 
     { 
      "name": "Jacob", 
      "instrument:" "...", 
      "manager": { 
       "name": "Cole" 
      } 
     } 
    ] 
} 
+1

또한 'angular.fromJson' 함수가 있지만 거의 동일한 것입니다 : 'return isString (json)? JSON.parse (json) : json;' –