在Angular Js中解析JSON

时间:2018-03-15 12:19:10

标签: javascript html angularjs

我需要在angularJS中使用Parse到json。

 {
    "status": true,
    "previous_status": "",
    "pass": true,
    "name": "N/A",
    "payment_date": "March 5, 2018 - 1:54 pm",
    "address": "N/A",
    "city": "N/A",
    "state": "N/A",
    "country": "N/A",
    "checksum": "1009-1",
    "custom_fields": [
        [
            "Ticket Type",
            "Friday ticket - 2018"
        ],
        [
            "Buyer Name",
            "Jhon Doe"
        ],
        [
            "Buyer E-mail",
            "demo@email.com"
        ]
    ]
}

我做 ng-repeat 但是在 HTML

中显示

["门票类型","星期五门票"]

["买方名称"," Jhon Doe"]

["买方电子邮件"," demo@email.com"]

我需要这样的。

门票类型:周五门票

代码段:

function fetch() {
  $http({
    method: 'GET',
    url: url,
    timeout: 6000
  }).then(
    function(result) {
      $scope.tickets = result.data;
    }
  );

}

1 个答案:

答案 0 :(得分:2)

您需要先将该数组映射到HTML中。

此备选方案不会改变原始数据。



var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.custom_fields = [
    ["Ticket Type", "Friday ticket - 2018"],
    ["Buyer Name", "Jhon Doe"],
    ["Buyer E-mail", "demo@email.com"]
  ];
  $scope.mapped = $scope.custom_fields.map(function(c) {
    return {
      title: c[0],
      text: c[1]
    };
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller='myCtrl'>
  <p ng-repeat='entry in mapped'>
    <b>{{entry.title}}</b>: {{entry.text}}
  </p>
</div>
&#13;
&#13;
&#13;