使用JSON.NET ASP.Net/AngularJS序列化对象时,防止$ id / $ ref

时间:2016-02-29 15:55:57

标签: javascript asp.net angularjs json json.net

在序列化对象时,我似乎无法阻止Web API / JSON.NET使用Newtonsoft.Json.PreserveReferencesHandling.Objects。换句话说,尽管使用了以下设置,但$ id / $ ref总是在序列化对象中使用:



.controller("etudCtrl", ["$scope", "$http",
           function($scope, $http) {
             var init;

             $scope.errors = [];
             $scope.msgs = [];

             $http({
                 method: 'GET',
                 url: 'MyURL/api/Students'
               })
               .success(function(data) {
                   var result = data;


                   //this method reads references but I get with it 3 Students only 
                   var key = "Student ";
                   var log = [];
                   var i = 0;
                   angular.forEach(data, function(dataa, key) {
                     var data1 = getJsonNetObject(dataa, data);
                     result[i] = data1;
                     i++;

                   }, log);


                   debugger;

                   $scope.currentPageStores = result;


                   //method to read references in JSON Data

                   function getJsonNetObject(obj, parentObj) {
                     // check if obj has $id key.
                     var objId = obj["$id"];
                     if (typeof(objId) !== "undefined" && objId != null) {
                       // $id key exists, so you have the actual object... return it
                       return obj;
                     }
                     // $id did not exist, so check if $ref key exists.
                     objId = obj["$ref"];
                     if (typeof(objId) !== "undefined" && objId != null) {
                       // $ref exists, we need to get the actual object by searching the parent object for $id
                       return getJsonNetObjectById(parentObj, objId);
                     }
                     // $id and $ref did not exist... return null
                     return null;
                   }

                   // function to return a JSON object by $id
                   // parentObj: the top level object containing all child objects as serialized by JSON.NET.
                   // id: the $id value of interest
                   function getJsonNetObjectById(parentObj, id) {
                     // check if $id key exists.
                     var objId = parentObj["$id"];
                     if (typeof(objId) !== "undefined" && objId != null && objId == id) {
                       // $id key exists, and the id matches the id of interest, so you have the object... return it
                       return parentObj;
                     }
                     for (var i in parentObj) {
                       if (typeof(parentObj[i]) == "object" && parentObj[i] != null) {
                         //going one step down in the object tree
                         var result = getJsonNetObjectById(parentObj[i], id);
                         if (result != null) {
                           // return found object
                           return result;
                         }
                       }
                     }
                     return null;
                   }

<table ng-controller="etudCtrl">
  <thead></thead>
  <tbody>
    <tr ng-repeat="store in currentPageStores">
      <td align="center">{{store.LastName}}</td>
      <td align="center">{{store.FirstName}}</td>
      <td align="center">{{store.Email}}</td>
      <td align="center">{{store.Class.Libel}}</td>
  </tbody>
  </table
&#13;
&#13;
&#13;

这是我得到的结果: enter image description here 我只有3名学生,但是当我调试时,我得到了所有9名学生的结果&#34;变量

这是JSON数据:http://www.jsoneditoronline.org/?id=8ec056773f4f5bd6fff077d27d2f08b5/

我怎样才能更正我的代码,将9名学生的名单发送到表格,不仅仅是3 谢谢你的帮助

0 个答案:

没有答案