从引用的JSON表单中获取特定数据

时间:2015-10-27 14:31:59

标签: json angularjs asp.net-mvc-5

我有一个json数据,每个对象都像这个例子一样被引用:

 [
        {
        $id: "1",
        FirstName: "Student ",
        LastName: "1",
        Email: "student1@yahoo.com",
        Class: {
            $id: "2",
            Libel: "Class 1",
            LevelId: 1,
            Students: [
             {
               $ref: "1"
             },
             {
               $id: "3",
               FirstName: "Student ",
               LastName: "2",
               Email: "student2@yahoo.com",
               Class: {
                  $ref: "2"
               },
               Id: 2,
               ObjectState: 0
             },
             { $id: "5",
               FirstName: "Student ",
               LastName: "3",
               Email: "student3@yahoo.com",
               Class: {
               $ref: "2"
               },
             }
               Id: 3,
               ObjectState: 0
            },
            Id: 1,
            ObjectState: 0
            } ...//other students
            ],
        Id: 1,
        ObjectState: 0
        },
        {
          $ref: "4"
        },  
        {
          $ref: "12"
            }
               ....
              ]


Each student has a class,and each class has a student,each object has a reference, so I have a problem in getting the data of students(like the lastName and FirstName,Email for each one) and display it in a table,I work with ASP.NET for creating REST Service and AngularJS for the client,

这是我的代码:  控制器:

.controller("etudCtrl", ["$scope", "$http",function ($scope, $http) {
 $http({method: 'GET', url: 'http://localhost:50001/api/Students'})
   .success(function (data) {
      $scope.currentPageStores = data; // response data 
      console.log("success");
    }
    })
    .error(function (data, status, headers, config) {
      console.log("data error ...");
                            }); 
}

我的HTML代码:

 <div ng-controller="etudCtrl">
       <table ng-repeat="store in currentPageStores>
          <td>{{store.LastName}}</td>
          <td>{{store.FirstName}}</td>
          <td>{{store.Email}}</td> 
          <td>{{store.Class.Libel}}</td>
       </table>
    </div>

结果我在桌子上得到了什么:

enter image description here

脚本执行的恢复:

Local
    data: Array[9]
    0: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    length: 9 //I have 9 students in my base
    __proto__: Array[0]



 In the console I get the correct data for each student, but the problem I think with filtering Student data in my controller
    any help is appreciated.

由于

更新:      我使用Include方法将Class实体链接到Student实体,如下所示:

//StudentsController:

        [Route("api/Students")]
            public IEnumerable<Student> Get()
            {
                return _StudentService.GetStudentsWithClass().ToList();
            }

//StudentService

    public IQueryable<Student> GetStudentsWithClass()
            {
       return _repository.Queryable().Include(s => s.Class);
            }

我有9名学生,因此获得学生名单de学生我只得到其中三名,其他人被班级引用!正如我在json结构中所展示的那样,它没有让学生进入类对象,引用的对象 是否有任何过滤器应用于控制器只过滤学生

   all json output:

 [
{
$id: "1",
FirstName: "Student ",
LastName: "1",
Email: "student1@yahoo.com",
Class: {
$id: "2",
Libel: "Class 1",
Level: null,
LevelId: 1,
Students: [
{
$ref: "1"
},
{
$id: "3",
FirstName: "Student ",
LastName: "2",
Email: "student2@yahoo.com",
Class: {
$ref: "2"
},
ClassId: 1,
Image: {
$id: "4",
Url: "URL Image 2",
Alt: "Image 2",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "3"
}
],
Id: 2,
ObjectState: 0
},
ImageId: 2,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 2,
ObjectState: 0
},
{
$id: "5",
FirstName: "Student ",
LastName: "3",
Email: "student3@yahoo.com",
Class: {
$ref: "2"
},
ClassId: 1,
Image: {
$id: "6",
Url: "URL Image 3",
Alt: "Image 3",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "5"
}
],
Id: 3,
ObjectState: 0
},
ImageId: 3,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 3,
ObjectState: 0
},
{
$id: "7",
FirstName: "Student ",
LastName: "4",
Email: "student4@yahoo.com",
Class: {
$ref: "2"
},
ClassId: 1,
Image: {
$id: "8",
Url: "URL Image 4",
Alt: "Image 4",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "7"
}
],
Id: 4,
ObjectState: 0
},
ImageId: 4,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 4,
ObjectState: 0
}
],
Schedules: [ ],
Exams: [ ],
Id: 1,
ObjectState: 0
},
ClassId: 1,
Image: {
$id: "9",
Url: "URL Image 1",
Alt: "Image 1",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "1"
}
],
Id: 1,
ObjectState: 0
},
ImageId: 1,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 1,
ObjectState: 0
},
{
$ref: "3"
},
{
$ref: "5"
},
{
$ref: "7"
},
{
$id: "10",
FirstName: "Student ",
LastName: "5",
Email: "student5@yahoo.com",
Class: {
$id: "11",
Libel: "Class 2",
Level: null,
LevelId: 2,
Students: [
{
$ref: "10"
},
{
$id: "12",
FirstName: "Student ",
LastName: "6",
Email: "student6@yahoo.com",
Class: {
$ref: "11"
},
ClassId: 2,
Image: {
$id: "13",
Url: "URL Image 6",
Alt: "Image 6",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "12"
}
],
Id: 6,
ObjectState: 0
},
ImageId: 6,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 6,
ObjectState: 0
},
{
$id: "14",
FirstName: "Student ",
LastName: "7",
Email: "student7@yahoo.com",
Class: {
$ref: "11"
},
ClassId: 2,
Image: {
$id: "15",
Url: "URL Image 7",
Alt: "Image 7",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "14"
}
],
Id: 7,
ObjectState: 0
},
ImageId: 7,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 7,
ObjectState: 0
}
],
Schedules: [ ],
Exams: [ ],
Id: 2,
ObjectState: 0
},
ClassId: 2,
Image: {
$id: "16",
Url: "URL Image 5",
Alt: "Image 5",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "10"
}
],
Id: 5,
ObjectState: 0
},
ImageId: 5,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 5,
ObjectState: 0
},
{
$ref: "12"
},
{
$ref: "14"
},
{
$id: "17",
FirstName: "Student ",
LastName: "8",
Email: "student8@yahoo.com",
Class: {
$id: "18",
Libel: "Class 3",
Level: null,
LevelId: 3,
Students: [
{
$ref: "17"
},
{
$id: "19",
FirstName: "sam",
LastName: "9",
Email: "student9@yahoo.com",
Class: {
$ref: "18"
},
ClassId: 3,
Image: {
$id: "20",
Url: "URL Image 8",
Alt: "Image 8",
SchoolHeads: [ ],
Teachers: [ ],
Feeds: [ ],
Students: [
{
$ref: "17"
},
{
$ref: "19"
}
],
Id: 8,
ObjectState: 0
},
ImageId: 8,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 9,
ObjectState: 0
}
],
Schedules: [ ],
Exams: [ ],
Id: 3,
ObjectState: 0
},
ClassId: 3,
Image: {
$ref: "20"
},
ImageId: 8,
Marks: [ ],
Punishments: [ ],
Attendances: [ ],
ParentHasStudent: [ ],
StudentComments: [ ],
Id: 8,
ObjectState: 0
},
{
$ref: "19"
}
]

我用于api的配置(webApiconfig.cs)

 var json = config.Formatters.JsonFormatter;
 json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
 config.Formatters.Remove(config.Formatters.XmlFormatter);

0 个答案:

没有答案