JSON无法识别实体框架数组

时间:2015-07-12 20:08:54

标签: javascript c# json entity-framework

您好我正在使用Entity Framework使用

使用存储库获取数据
 var test=DbContext.Set<tHoliday>.Include("tInfo"); 
   return test.AsQueryable(); 

其中提供了以下数据 数据格式很好

    {
    •   $id: "1",
    •   $values: 
    [
    o   {
        $id: "2",
        id: 1,
        shcdName: "Main",
       t_holidays: 
    {
        $id: "3",
        $values: 
    [
       {
       $id: "4",
       id: 13,
       description: "Good Friday",
       t_holiday_schd_nameId: 1,
       date: "2105-04-06T00:00:00",
       t_holiday_schd_name: 
    {
       $ref: "2"
    }
       },
       {
       $id: "5",
       id: 14,
       description: "Memorial Day for 2015",
       t_holiday_schd_nameId: 1,
       date: "2105-05-28T00:00:00",
       t_holiday_schd_name: 
    {
       $ref: "2"
    }
       }, }
]
}
o   },
o   {
   $id: "16",
   id: 3,
   shcdName: "Night_2015",
   t_holidays: 
{
   $id: "17",
   $values: [ ]
}
o   },
o   {
   $id: "18",
   id: 2,
   shcdName: "Swing_2015",
   t_holidays: 
{
   $id: "19",
   $values: 
[
   {
   $id: "20",
   id: 1,
   description: "Good Friday",
   t_holiday_schd_nameId: 2,
   date: "2105-04-06T00:00:00",
   t_holiday_schd_name: 
{
   $ref: "18"
}
   },
   {
   $id: "21",
   id: 2,
   description: "Memorial Day for 2015",
   t_holiday_schd_nameId: 2,
   date: "2105-05-28T00:00:00",
   t_holiday_schd_name: 
{
   $ref: "18"
}


]
}
o   }
]
}

在我的前端我使用javascript库获取此数组时它不会识别它是JSON数组并给出以下错误

Error: [$resource:badcfg] Error in resource configuration for action `query`. Expected response to contain an array but got an object

请告诉我如何将其转换为JSON数组或在EF中获取JSON数组谢谢

1 个答案:

答案 0 :(得分:0)

您的数组(您显示的数据)包含在顶级对象

{...}

在服务器端,我认为只要离开.AsQueryable调用并返回DbContext.Set.Include(“tInfo”)就可以了。

在前端,您可以在资源中使用transformResponse函数:

angular.module('services', ['ngResource']).
factory("someService", function ($resource) {
    return $resource(
        '/', {}, {
        get: {
            method: 'GET',
            transformResponse: function(data, headers){

                return data.$values;
            }
        }
    }

    );
});