如何序列为JSON循环引用的对象结构? [英] How to serialize as Json an object structure with circular references?
本文介绍了如何序列为JSON循环引用的对象结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象的结构是这样的:
I have an object structure like this:
public class Proposal {
public List<ProposalLine> Lines { get; set; }
public string Title { get; set; }
}
public class ProposalLine {
public Proposal Proposal { get; set; } // <- Reference to parent object
}
我尝试序列议案为JSON,它告诉我,有一个循环引用,这是正确的。结果
不幸的是,我不能碰的对象,因为它们是从其他项目中引用的DLL - 否则我会改变他们。
I try to serialize Proposal as Json, it tells me that there is a circular reference, which is correct.
Unfortunately, I can't touch the objects, since they are in a referenced DLL from another project - otherwise I'd change them.
有没有办法来序列为JSON而忽略循环特性?
Is there a way to serialize as Json and ignore the circular properties?
推荐答案
使用的Newtonsoft.Json(这是默认的.NET JSON序列化),并设置
Use the Newtonsoft.Json (which is the default .net json serializer) and set
JsonSerializerSettings settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
var serializer = JsonSerializer.Create(settings);
如果您正在开发MVC应用也可以全局定义这个变量...
You can also globally define this variable if you are developing MVC applications...
这篇关于如何序列为JSON循环引用的对象结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文