Asp.Net Core Web Api,复杂对象中的Json对象未返回子对象

6

我有一个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 相关的信息。
我的代码有问题吗?
谢谢。

1
你使用EF吗?在检索数据的方法中,你是否忘记了包含“Include”员工?https://learn.microsoft.com/en-us/ef/core/querying/related-data/ - Sergey Nazarov
员工列表的jsonarray的键是listEmployees吗? - Rajan Pipaliya
我没有使用include,而是使用Linq请求来填充对象,对象已经被填充了,但在Json对象中并没有显示“listEmployees”列表。 - devjelop anje
展示 IRepos.GetList 代码。 - Farhad Zamani
你能否调试一下,看看在转换为JSON之前,“ilist”是否包含任何员工? - Tan
我添加了IRepos.GetList函数。 - devjelop anje
3个回答

7

我找到了解决方案,并发布此方案以解决此类问题。确实需要在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; };
}

敬礼


这应该是这里的采纳答案。谢谢! - undefined

3
需要挖掘一些才能找到,但是您的问题在于listEmployees是一个字段。目前,System.Text.Json仅支持 属性的序列化。您需要将listEmployees更改为属性或使用Newtonsoft.Json相关

3
我猜想您在使用System.Text.Json库来序列化和反序列化.NET Core应用程序中的数据,对吗?如果是这种情况,可能问题与该库有关。据我所知,当我们使用System.Text.Json库序列化复杂对象时,它只会返回外部对象(没有内部实体)。
为解决此问题,您可以尝试使用Microsoft.AspNetCore.Mvc.NewtonsoftJson库进行数据序列化。请参考以下步骤:
  1. Install Microsoft.AspNetCore.Mvc.NewtonsoftJson package via Nuget.

  2. Register the NewtonsoftJson in the Startup.ConfigureServices method:

         services.AddControllersWithViews().AddNewtonsoftJson();
    
以下是关于System.Text.JsonMicrosoft.AspNetCore.Mvc.NewtonsoftJson的相关文章,您可以参考它们:

.NET中如何序列化和反序列化JSON

从Newtonsoft.Json迁移到System.Text.Json的方法


我使用了System.Text.Json更改了方法,但问题仍然存在,listEmployees列表没有在Json对象中返回。以下是代码: - devjelop anje
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };IList<DepartementViewModel> ilIst = await IRepos.GetList(); var modelJson = JsonSerializer.Serialize(ilIst, options); return Ok(modelJson); - devjelop anje
是的,我知道。我已经根据您的代码创建了一个示例并重现了这个问题。如果我使用 System.Text.Json,它会显示此问题。在更改为使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson 库后,一切都正常工作。因此,我建议您尝试使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson 库,而不是 'System.Text.Json'。 - Zhi Lv
我找到了解决方案,我应该为listEmployees属性添加Getter和Setter。 - devjelop anje

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接