相关疑难解决方法(0)

双向链表到 JSON

我有一个三维结构......实际上是一个双向链表,有六个节点,即左、右、上、下、进、出。如果一个节点在另一个节点的右侧,那么该节点将明显位于第一个节点的左侧。喜欢

3D 实际数据结构的 2D 样本

实际上这是一个 3D 结构,但为了便于理解,我给出了一个 2D 示例。现在我必须将其转换为 JSON 格式,以便通过 WCF 将此数据发送到客户端,但由于它包含循环,因此无法将其转换为 JSON。我有这些问题

  1. 这种双向链表可以转成JSON吗?
  2. 有没有另一种方法可以做到这一点?
  3. 任何其他推荐的数据结构?如果使用双向链表这是不可能的。

我正在使用Json.Net来处理 JSON。

我的班级是

public class Node
{
    public Document document = null;

    public Node left = null;
    public Node right = null;
    public Node up = null;
    public Node down = null;
    public Node inside = null;
    public Node outside = null;
}
Run Code Online (Sandbox Code Playgroud)

c# json json.net data-structures doubly-linked-list

6
推荐指数
1
解决办法
3097
查看次数

使用Newtonsoft JSON处理循环引用

我的模型类如下所示:

public class ModelType
{
    public string Name { get; set; }
    public ModelType SuperType { get; set }
    public IEnumerable<ModelType> SubTypes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我试图序列化对象,但得到StackOverflowException.我试着打电话

JsonConvert.SerializeObject(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)

以及

JsonConvert.SerializeObject(model, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
Run Code Online (Sandbox Code Playgroud)

这两个电话都导致了StackOverflowException.知道如何序列化ModelType实例吗?

编辑:

实例示例,无法序列化:

{
    Name: "Child",
    SuperType: {
        Name: "Parent",
        SuperType: null,
        SubTypes: [{
                Name: "Child",
                SuperType: {
                    Name: "Parent",
                    SuperType: null,
                    SubTypes: [{Name: "Child", ...}]
                },
                SubTypes: …
Run Code Online (Sandbox Code Playgroud)

c# serialization json json.net

6
推荐指数
1
解决办法
1万
查看次数