双向链接列表到JSON [英] Doubly Linked List to JSON

查看:176
本文介绍了双向链接列表到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个三维结构...实际上是一个双向链接列表,具有六个节点,即左,右,上,下,内,外。如果一个节点位于其他节点的右侧,那么该节点将在第一个节点的左侧被拒绝。像





实际上这是一个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;
}


解决方案

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

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

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



演示:

  using System; 
使用System.Collections.Generic;
使用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 =House 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 = JsonConvert.DeserializeObject&NodeB(json,settings);

Console.WriteLine();

DumpNodes(node);
}

private static void DumpNodes(Node startingNode)
{
HashSet< Node> seen = new HashSet< 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>看到)
{
if(node!= null)
{
Console.WriteLine(+ dir +:+ node.Name);
if(!seen.Contains(node))
{
queue.Add(node);
}
}
}
}

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

输出:

 在房子
西:房子前面
东:房子的后面
房子前面
北:房子北
东:在房子
南:房子南
房子后面
北:房子北
西:房子
南:房子南$ $ $ $ b北部的房子
西部:房子前面
东部:房子的后面
房子的南部
西部:房子前面
东部:房子的后面

{
$ id:1,
名称:内部,
东:{
$ id 2,
姓名:回屋,
北:{
$ id:3,
名称:北的房子,
东​​:{
$ ref:2
},
西:{
$ id: 4,
名称:前面的房子,
北:{
$ ref:3
},
南:{
$ id:5,
名称: 南楼,
东:{
$ ref:2
},
西:{
$ ref :4
}
},
东:{
$ ref:1
}
}
},
南:{
$ ref:5
},
西:{
$ ref:1
}
},
西:{
$ ref:4
}
}

在房子
西部:房子前面
东部:房子后面
房子前面
北部:房子北部
东部:在房子
南部:南面的房子
房子的后面
北:房子北
西:房子
南:房子南
房屋北
西:前的房子
东:房子的后面
房子的南部
西部:房子前面
东部:房子的后面

工作小提琴在这里: https:// dotne tfiddle.net/EojsFA


I've a three dimensional structure ... actually a doubly linked list with six nodes i.e. left, right, up, down, in, out. if one node is on the right side of other then that node will be defiantly on the left side of the first one. like

Actually this is a 3D structure, but for understanding purposes, I've given a 2D example. Now I've to convert it in JSON format, to send this data through WCF to a client, but as it contains loops so it couldn't be converted to JSON. I've these questions

  1. Can this type of doubly linked list be converted to JSON?
  2. Is there another way to do that?
  3. Any Other recommended Data Structure? If this is impossible using Doubly Linked List.

I'm using Json.Net to handle JSON.

My class is

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

解决方案

Json.Net can handle reference loops if you set the PreserveReferencesHandling option in the settings.

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

This setting will cause the JSON to be written with special $id and $ref properties which allow the JSON to be deserialized back to the original references, assuming you are using Json.Net to deserialize on the client side. With this in place you should be able to use your existing object structure with no problem.

Demo:

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

Output:

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

Working fiddle here: https://dotnetfiddle.net/EojsFA

这篇关于双向链接列表到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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