我如何解析此 JSON 响应

How can I parse this JSON response

angularJS 收到了以下 JSON 数据。它是使用 get 请求从 ArrayList 中引入的。

    0: Object
        id:1
        area: "State"
        gender: "Both"
        highestEd: 3608662
        .... etc

    1: Object
        id:2
        area: "State"
        gender: "Both"
        highestEd: 3608662
        .... etc

但是我不知道如何使用控制器或 UI 访问它。我希望能够将这些数据存储在 Angular 中,这样我就可以使用这些信息创建图表。我知道肯定存在的唯一方法是查看 firefox 中的响应。

任何帮助/建议将不胜感激。

  1. 您的 JSON 数据格式不正确。使用JSON lint检查
  2. 使用 $http.get('/get_url/') 获取响应。 One sample example for this
  3. 一旦您在 $scope.yourVariable 中有响应,请使用 ng-repeat 循环处理它

看看这个 fiddler here 你可能会有一些想法。

在使用您工厂的 $http 调用 API 后,您将解析所获取的数据。 controller需要调用API方法,然后将你factory解析出来的数据绑定到API.

工厂

myApp.factory("Data", function($q, $http) {
  return {
    getData: function() {
      var deferred = $q.defer();
      /* --Actual API Call
            $http.get("/url")
        .then(function(response) {
          deferred.resolve(response.data);
        })
                */
                //Dummy Data
      var data = m;
      deferred.resolve(data);
      return deferred.promise;
    }
  }
});

控制器

function MyCtrl($scope, Data) {
  Data.getData()
    .then(function(items) {
      $scope.bobs = items;
      console.log($scope.bobs);
    })
}

将工厂中的虚拟数据初始化替换为实际的 API 调用。