我有一个这样的对象结构:
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
}
我尝试将 Proposal 序列化为 Json,它告诉我存在循环引用,这是正确的。
不幸的是,我无法触摸这些对象,因为它们在另一个项目的引用 DLL 中 - 否则我会更改它们。
有没有办法序列化为 Json 并忽略循环属性?
最佳答案
使用 Newtonsoft.Json(这是默认的 .net json 序列化程序)并设置
JsonSerializerSettings settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
var serializer = JsonSerializer.Create(settings);
如果您正在开发 MVC 应用程序,您还可以全局定义此变量...
关于c# - 如何将具有循环引用的对象结构序列化为 Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818386/