【问题标题】:Asp.Net Core Web Api, Json Object not return child objects in Complex objectAsp.Net Core Web Api,Json 对象不返回复杂对象中的子对象
【发布时间】:2020-10-05 12:43:29
【问题描述】:

我有 Asp.Net core 3.1 Web Api,其中一个复杂的对象应该由 Json 之类的操作返回,问题是返回的对象不包含子对象列表:

物体下方:

public class DepartementViewModel
{
    public int Dep_ID { get; set; }
    public string Dep_Name { get; set; }

    public List<BasicEmpViewModel> listEmployees = new List<BasicEmpViewModel>();
}

还有行动

[HttpGet]
public async Task<ActionResult<IEnumerable<DepartementViewModel>>> GetDepartement()
{
    IRepository IRepos = new DepartementRepository(_context);
    IList<DepartementViewModel> ilIst = await IRepos.GetList();
    return Ok(ilIst);
}

仓库GetList函数

public async Task<IList<DepartementViewModel>> GetList()
{
    IList<DepartementViewModel> listDept = new List<DepartementViewModel>();
    listDept = await(from dept in _context.Departement                        
                          orderby dept.Dep_ID ascending
                              select new DepartementViewModel
                              {
                                  dept.Dep_ID ,
                                  dept.Dep_Name
                              }
                     ).ToListAsync();

    listDept.ForEach(x =>
    {       
        var emObj =_context.Employee;
        foreach (Employee E in emObj)
        {
            E.listEmployees.Add(new BasicEmpViewModel()
                                    {
                                        Emp_ID = E.Emp_ID,
                                        Emp_Name = E.Emp_Name,
                                        Checked = (E.Dep_ID == x.Dep_ID) ? true : false
                                    }
                                );
        }
    }); 

    return listDept;
}

返回的 Json 对象不包含员工列表“listEmployees”,它只显示与主要对象相关的信息:Dep_ID 和 Dep_Name。

我的代码中是否缺少某些内容?

谢谢

【问题讨论】:

  • 你使用EF吗?您是否忘记了检索数据的方法中的Include 员工? docs.microsoft.com/en-us/ef/core/querying/related-data
  • 员工列表key的jsonarray是listEmployees吗?
  • 我没有使用 include 但我使用 Linq 请求来填充对象并且对象填充良好但它没有在 Json 对象中显示列表“listEmployees”
  • 显示IRepos.GetList代码
  • 在转换为json之前,你能调试一下“ilist”是否包含任何雇员吗?

标签: c# asp.net-mvc asp.net-core asp.net-core-webapi


【解决方案1】:

我找到了解决方案,我发布了任何此类问题的解决方案。确实有必要在 DepartementViewModel 类中为属性 listEmployees 放置一个 Getter 和 Setter,如下所示

public class DepartementViewModel
{
    public int Dep_ID { get; set; }
    public string Dep_Name { get; set; }

    public List<BasicEmpViewModel> listEmployees {get; set; };
}

真诚的

【讨论】:

  • 感谢您回答您的问题,我也花了一段时间。
【解决方案2】:

我想也许您正在使用System.Text.Json 库来序列化和反序列化 .net 核心应用程序中的数据,对吧?如果是这样的话,也许问题与这个库有关。据我所知,当我们使用System.Text.Json库序列化复杂对象时,它只会返回外部对象(没有内部实体)。

要解决这个问题,您可以尝试使用Microsoft.AspNetCore.Mvc.NewtonsoftJson 库来序列化数据。参考以下步骤:

  1. 通过 Nuget 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包。

  2. 在 Startup.ConfigureServices 方法中注册NewtonsoftJson

         services.AddControllersWithViews().AddNewtonsoftJson();
    

这里有一些关于System.Text.JsonMicrosoft.AspNetCore.Mvc.NewtonsoftJson的相关文章,你可以参考一下:

How to serialize and deserialize (marshal and unmarshal) JSON in .NET

How to migrate from Newtonsoft.Json to System.Text.Json

【讨论】:

  • 我改了使用 System.Text.Json 的方法,但问题依旧,代码下面的 Json 对象中没有返回列表 listEmployees:
  • var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; IList ilList = await IRepos.GetList(); var modelJson = JsonSerializer.Serialize(ilIst, options);返回确定(modelJson);
  • 是的,我知道。我根据您的代码创建了一个示例并重现了该问题。如果我使用System.Text.Json,它将显示此问题。更改为使用Microsoft.AspNetCore.Mvc.NewtonsoftJson 库后,一切正常。所以,我建议你尝试使用Microsoft.AspNetCore.Mvc.NewtonsoftJson 库,而不是'System.Text.Json'。
  • 我得到了解决方案,我应该将 getter 和 setter 用于 listEmployees 属性
【解决方案3】:

费了一番功夫才找到,但您的问题是listEmployees 是一个字段。目前System.Text.Json 仅支持serialization of properties。您需要将listEmployees 更改为属性或使用Newtonsoft.Json

Related

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2015-08-27
    相关资源
    最近更新 更多