gpt4 book ai didi

c# - 双向链表到 JSON

转载 作者:太空狗 更新时间:2023-10-30 01:19:10 27 4
gpt4 key购买 nike

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

2D sample of 3D actual data struture

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

  1. 这种双向链表能转成JSON吗?
  2. 还有其他方法吗?
  3. 还有其他推荐的数据结构吗?如果这是不可能的,则使用双向链表。

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

我的类(class)是

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;
}

最佳答案

如果您在设置中设置 PreserveReferencesHandling 选项,Json.Net 可以处理引用循环。

JsonSerializerSettings settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(rootNode, settings);

此设置将导致 JSON 写入特殊的 $id$ref 属性,这允许将 JSON 反序列化回原始引用,假设您是使用 Json.Net 在客户端反序列化。有了这个,您应该能够毫无问题地使用现有的对象结构。

演示:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
public static void Main()
{
Node center = new Node { Name = "In House" };
Node north = new Node { Name = "North of House" };
Node west = new Node { Name = "Front of House" };
Node east = new Node { Name = "Back of House" };
Node south = new Node { Name = "South of House" };

center.East = east;
east.West = center;

center.West = west;
west.East = center;

east.North = north;
north.East = east;

east.South = south;
south.East = east;

south.West = west;
west.South = south;

west.North = north;
north.West = west;

DumpNodes(center);

Console.WriteLine();

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
settings.NullValueHandling = NullValueHandling.Ignore;
settings.Formatting = Formatting.Indented;

string json = JsonConvert.SerializeObject(center, settings);
Console.WriteLine(json);

Node node = JsonConvert.DeserializeObject<Node>(json, settings);

Console.WriteLine();

DumpNodes(node);
}

private static void DumpNodes(Node startingNode)
{
HashSet<Node> seen = new HashSet<Node>();
List<Node> queue = new List<Node>();
queue.Add(startingNode);
while (queue.Count > 0)
{
Node node = queue[0];
queue.RemoveAt(0);
if (!seen.Contains(node))
{
seen.Add(node);
Console.WriteLine(node.Name);
Look("north", node.North, queue, seen);
Look("west", node.West, queue, seen);
Look("east", node.East, queue, seen);
Look("south", node.South, queue, seen);
}
}
}

private static void Look(string dir, Node node, List<Node> queue, HashSet<Node> seen)
{
if (node != null)
{
Console.WriteLine(" " + dir + ": " + node.Name);
if (!seen.Contains(node))
{
queue.Add(node);
}
}
}
}

public class Node
{
public string Name { get; set; }
public Node North { get; set; }
public Node South { get; set; }
public Node East { get; set; }
public Node West { get; set; }
}

输出:

In House
west: Front of House
east: Back of House
Front of House
north: North of House
east: In House
south: South of House
Back of House
north: North of House
west: In House
south: South of House
North of House
west: Front of House
east: Back of House
South of House
west: Front of House
east: Back of House

{
"$id": "1",
"Name": "In House",
"East": {
"$id": "2",
"Name": "Back of House",
"North": {
"$id": "3",
"Name": "North of House",
"East": {
"$ref": "2"
},
"West": {
"$id": "4",
"Name": "Front of House",
"North": {
"$ref": "3"
},
"South": {
"$id": "5",
"Name": "South of House",
"East": {
"$ref": "2"
},
"West": {
"$ref": "4"
}
},
"East": {
"$ref": "1"
}
}
},
"South": {
"$ref": "5"
},
"West": {
"$ref": "1"
}
},
"West": {
"$ref": "4"
}
}

In House
west: Front of House
east: Back of House
Front of House
north: North of House
east: In House
south: South of House
Back of House
north: North of House
west: In House
south: South of House
North of House
west: Front of House
east: Back of House
South of House
west: Front of House
east: Back of House

在这里工作:https://dotnetfiddle.net/EojsFA

关于c# - 双向链表到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com