2015-07-02 4 views
0

У меня есть объект JSON в нижнем формате. В настоящее время это жестко закодировано. Тем не менее, я хотел бы динамически создать этот JSON.Создайте объект JSON динамически Угловой

У меня есть еще один объект, который мне нужно перебрать, а затем сформировать этот JSON объект

[ 
{ 
           name: "test1", 
           enable: true, 
           unit: 24350, 
           unit_price: 1.0368, 
           composition: [ 
           { 
            asset: "asset1", 

            percentage: 15 
           }, 
           { 
            asset: "asset2", 

            percentage: 10 
           }, 
           { 
            asset: "asset3", 

            percentage: 5 
           }, 
           { 
            asset: "asset4", 

            percentage: 35 
           }, 
           { 
            asset: "asset5", 

            percentage: 20 
           }, 
           { 
            asset: "asset6", 

            percentage: 15 
           } 
           ] 
          }, 
{ 
           name: "test2", 
           enable: true, 
           unit: 24350, 
           unit_price: 1.0368, 
           composition: [ 
           { 
            asset: "asset1", 

            percentage: 15 
           }, 
           { 
            asset: "asset2", 

            percentage: 10 
           }, 
           { 
            asset: "asset3", 

            percentage: 5 
           }, 
           { 
            asset: "asset4", 

            percentage: 35 
           }, 
           { 
            asset: "asset5", 

            percentage: 20 
           }, 
           { 
            asset: "asset6", 

            percentage: 15 
           } 
           ] 
          }] 

Спасибо заранее.

ответ

0

Просто используйте родной Javascript JSON.parse

var data = [ 
    { 
     name: "test1", 
     enable: true, 
     unit: 24350, 
     unit_price: 1.0368, 
     composition: [ 
     { 
      asset: "asset1", 

      percentage: 15 
     }, 
     { 
      asset: "asset2", 

      percentage: 10 
     }, 
     { 
      asset: "asset3", 

      percentage: 5 
     }, 
     { 
      asset: "asset4", 

      percentage: 35 
     }, 
     { 
      asset: "asset5", 

      percentage: 20 
     }, 
     { 
      asset: "asset6", 

      percentage: 15 
     } 
     ] 
    }, 
    { 
     name: "test2", 
     enable: true, 
     unit: 24350, 
     unit_price: 1.0368, 
     composition: [ 
     { 
      asset: "asset1", 

      percentage: 15 
     }, 
     { 
      asset: "asset2", 

      percentage: 10 
     }, 
     { 
      asset: "asset3", 

      percentage: 5 
     }, 
     { 
      asset: "asset4", 

      percentage: 35 
     }, 
     { 
      asset: "asset5", 

      percentage: 20 
     }, 
     { 
      asset: "asset6", 

      percentage: 15 
     } 
     ] 
    }] 

var json = JSON.parse(data); 
+0

Привет Andi, Я хочу, чтобы создать этот объект не разбирать его. – Frenz

+0

Какой вклад? Строка или что-то еще? –

0

function LoginController($scope) { 
 
    $scope.post = [{ 
 
     "name": "Shaw", 
 
     "unit_price": 1586, 
 
     "enable": true, 
 
     "composition": [{ 
 
     "asset": "asset1" 
 
     }, { 
 
     "percentage": 15 
 
     }] 
 
    }, { 
 
     "name": "Allen", 
 
     "unit_price": 1586, 
 
     "enable": false, 
 
     "composition": [{ 
 
     "asset": "asset2" 
 
     }, { 
 
     "percentage": 30 
 
     }] 
 
    } 
 

 
    ]; 
 
    $scope.data = []; 
 
    var tempObj1 = []; 
 
    for (i = 0; i < $scope.post.length; i++) { 
 
    var tempObj1 = { 
 

 
     "name": $scope.post[i].name, 
 
     "unit_price": $scope.post[i].unit_price, 
 
     "enable": $scope.post[i].enable, 
 

 

 
    }; 
 
    tempObj1.composition = []; 
 
    tempObj2 = { 
 
     "asset": $scope.post[i].composition[0].asset, 
 
     "percentage": $scope.post[i].composition[1].percentage 
 
    }; 
 
    tempObj1.composition.push(tempObj2); 
 

 
    $scope.data.push(tempObj1); 
 
    } 
 
    console.log('data', $scope.data); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> 
 
<div ng-app ng-controller="LoginController"> 
 
    <table> 
 
    <tr> 
 
     <th>Name</th> 
 
     <th>Unit_Price</th> 
 
     <th>Enable</th> 
 
     <th>Compostion 
 
     <table> 
 
      <th>asset</th> 
 
      <th>percentage</th> 
 
     </table> 
 
     </th> 
 
    </tr> 
 
    <tr ng-repeat="i in data"> 
 
     <td>{{ i.name }}</td> 
 
     <td>{{ i.unit_price }}</td> 
 
     <td>{{ i.enable }}</td> 
 
     <td>{{ i.composition[0].asset}}</td> 
 
     <td>{{ i.composition[0].percentage }}</td> 
 
    </tr> 
 
    </table> 
 
</div>

Смежные вопросы