JsonIgnore属性在ASP.NET工作吗? [英] JsonIgnore attributes not working in ASP.NET?

查看:262
本文介绍了JsonIgnore属性在ASP.NET工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的循环引用了项目的对象。我已经把[JsonIgnore]场以上,像这样:

I've got an object in my project with circular references. I've put [JsonIgnore] above the field like so:

    [JsonIgnore]
    public virtual Foobar ChildObject { get; set; }

我仍然得到循环引用错误,当我序列化对象。不具有JsonIgnore唯一字段是字符串字段,不应导致此。有没有别的东西,我需要做的就是JsonIgnore工作?

I'm still getting circular reference errors when I serialize the object. The only fields that do not have JsonIgnore are string fields and should not cause this. Is there something else I need to do to get JsonIgnore to work?

谢谢!

推荐答案

您可能有一个链接回其父一些其他财产。使用 ReferenceLoopHandling.Ignore 设置为prevent自引用循环。

You likely have some other property that links back to its parent. Use the ReferenceLoopHandling.Ignore setting to prevent self-referencing loops.

using Newtonsoft.Json;

JsonSerializerSettings jsSettings = new JsonSerializerSettings();
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

string json = JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings);

这篇关于JsonIgnore属性在ASP.NET工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆