动态创建JSON对象Angular

时间:2015-07-02 02:39:01

标签: javascript json angularjs

我有一个以下格式的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
                                }
                              ]
                          }]

提前致谢。

2 个答案:

答案 0 :(得分:1)



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);
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分: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);