首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新的ASP.NET核心3.0JSON序列化程序遗漏了数据

新的ASP.NET核心3.0JSON序列化程序遗漏了数据
EN

Stack Overflow用户
提问于 2019-10-09 14:01:38
回答 2查看 1K关注 0票数 1

我正在将一个web应用程序移植到ASP.NET核心3,经过一段时间的战斗,我几乎已经到达终点线。一切似乎都正常,但突然之间,从api返回的JSON数据缺少了一些级别。

看来options.JsonSerializerOptions.MaxDepth在64级是默认的,所以可能是这样的。还有别的地方可以拿我开玩笑吗?

这是代码(以及值的quickview ):

这就是我在浏览器中得到的JSON:

因此,生成的输出中完全缺少ParticipantGroups属性/集合。

有什么想法吗?

编辑:

我在Github上添加了一个回购程序来展示这个问题。标准的ASP.NET Core3.0解决方案,由模板创建,并对来自Weather预报控制器返回的结果进行更改:

https://github.com/steentottrup/systemtextjsonissue

EN

回答 2

Stack Overflow用户

发布于 2019-10-09 18:08:43

现在,我回到了使用Newtonsoft.Json和Microsoft.AspNetCore.Mvc.NewtonsoftJson包的问题上。然后,当我有时间时,我将尝试找出解决方案是什么,没有Newtonsoft.Json。

票数 2
EN

Stack Overflow用户

发布于 2019-10-10 05:59:21

问题似乎是新版本3.0中的一个错误。至少对我来说这似乎是个错误。

System.Text.Json似乎将转换层次结构中提到的类,而不是实际的类。因此,如果在层次结构中使用抽象类,则会遇到麻烦。第二次我删除了基类,并使用了我返回的实际类,问题似乎就消失了。

所以这不管用:

代码语言:javascript
复制
public class SurveyReportResult {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }
    public IEnumerable<OrganisationalUnit> OrganisationalUnits { get; set; }
}

public abstract class OrganisationalUnit {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }

}

public class OrganisationalUnitWithParticipantGroups : OrganisationalUnit {
    public IEnumerable<ParticipantGroup> ParticipantGroups { get; set; }
}

public class ParticipantGroup {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }
}

这将只返回OrganisationalUnit类的属性,而不是OrganisationalUnitWithParticipantGroups的附加属性。

这样做是可行的:

代码语言:javascript
复制
public class SurveyReportResult {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }
    public IEnumerable<OrganisationalUnitWithParticipantGroups> OrganisationalUnits { get; set; }
}

public class OrganisationalUnitWithParticipantGroups /*: OrganisationalUnit*/ {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }
    public IEnumerable<ParticipantGroup> ParticipantGroups { get; set; }
}

public class ParticipantGroup {
    public Guid Id { get; set; }
    public String Name { get; set; }
    public Int32 MemberCount { get; set; }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58305928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档