双向链表以JSON [英] Doubly Linked List to JSON

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

问题描述

我有一个三维结构......实际上是一个双向链表与六个节点,即左,右,上,下,IN,OUT。如果一个节点是对等的右侧则该节点将挑衅在第一个的左侧。像





其实这是一个三维结构,但理解的目的,我已经给了一个2D的例子。
现在我已经把它转换成JSON格式,通过WCF发送此数据到客户端,但因为它包含循环,因此它可能不会被转换为JSON。我有这些问题。




  1. 能否这类双向链表转换成JSON?

  2. 是否有另一种方式来做到这一点?

  3. 任何其他推荐的数据结构?如果采用双向链表是不可能的。



我使用的Json .NET 处理JSON。



我的班级

 公共类节点
{
公共文档文件= NULL;

公共节点离开= NULL;
公共节点右= NULL;
公共点向上= NULL;
公共节点向下= NULL;内部= NULL
公共节点; $ B外= NULL $ B公共节点;
}


解决方案

Json.Net可以处理参考循环如果设置中的设置 PreserveReferencesHandling 选项。

  JsonSerializerSettings设置=新JsonSerializerSettings 
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
格式化= Formatting.Indented
};
JSON字符串= JsonConvert.SerializeObject(rootNode中,设置);

这设置将导致JSON有特殊的$ id <写入/ code>和 $ REF 性质允许的JSON被反序列化回原来的引用,假设你使用Json.Net反序列化在客户端。 。有了这个地方,你应该能够没有问题使用现有对象结构



演示:

 使用系统; 
使用System.Collections.Generic;
使用Newtonsoft.Json;

公共类节目
{
公共静态无效的主要()
{
节点中心=新节点{名称=内训};
节点北=新节点{名称=府北};
节点西=新节点{名称=家门口};
节点东=新节点{名称=府后退};
节点南=新节点{名称=府南};

center.East =东;
east.West =中心;

center.West =西;
west.East =中心;

east.North =以北;
north.East =东;

east.South =南;
south.East =东;

south.West =西;
west.South =南;

west.North =以北;
north.West =西;

DumpNodes(中心);

Console.WriteLine();

JsonSerializerSettings设置=新JsonSerializerSettings();
settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
settings.NullValueHandling = NullValueHandling.Ignore;
settings.Formatting = Formatting.Indented;

JSON字符串= JsonConvert.SerializeObject(中心,设置);
Console.WriteLine(JSON);

节点node = JsonConvert.DeserializeObject<节点>(JSON,设置);

Console.WriteLine();

DumpNodes(节点);
}

私有静态无效DumpNodes(节点startingNode)
{
的HashSet<节点>可见=新的HashSet<节点>();
名单,LT;节点>队列=新的List<节点>();
queue.Add(startingNode);
,而(queue.Count大于0)
{
节点node =队列[0];
queue.RemoveAt(0);
如果
{
seen.Add(节点)(seen.Contains(节点)!);
Console.WriteLine(node.Name);
查找(北,node.North,排队,看到的);
查找(西,node.West,排队,看到的);
查找(东方,node.East,排队,看到的);
查找(南方,node.South,排队,看到的);
}
}
}

私有静态无效查找(字符串目录,节点节点列表<节点>队列,HashSet的<节点>看到)
{
如果(!节点= NULL)
{
Console.WriteLine(+方向+:+ node.Name);
如果
{
queue.Add(节点)(seen.Contains(节点)!);
}
}
}
}

公共类节点
{
公共字符串名称{;组; }
公共节点北{搞定;组; }
公共节点南{搞定;组; }
公共节点东{搞定;组; }
公共节点西{搞定;组; }
}



输出:

 内训
西:房子
对着东方:房子$ b $北:房子
北楼
前的回b东:在众议院
南:房子
西北方:在众议院
南:房子
返回众议院
北部的南府$ b $南b房子
西北方:房子
对着东方:返回众议院$ b $乙南府
西部:房子
对着东方:房子$ b $的回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
}
}

在众议院
西:房子
对着东方:众议院
前的回房子
北部:房子
东部北:在众议院
南:房子
西北方:房子
返回众议院
北部的南房子
南:房子
楼北
以西的南:房子
对着东方:返回众议院$ b $乙南府
西部:正面房子
东:房子
的后退

工作小提琴此处的 https://dotnetfiddle.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天全站免登陆