Title: directory 1
Level: 1
DesPageNum: 1
Title: directory
1.1Level: 2
DesPageNum: 2
Title: directory
1.2Level: 2
DesPageNum: 3
Title: 2 directory
Level: 1
DesPageNum: 4
Title: directory
2.1Level: 2
DesPageNum: 5
Title: directory 2.1.1
Level: 3
DesPageNum: 6
Title: directory 2.1.2
Level: 3
DesPageNum: 7
Title: directory
2.2Level: 2
11 desPageNum:
Into a hierarchical structure more apparent json
{
"SubBookMark" : [
{
"SubBookMark" : [],
"Title" : "1.1" directory,
"DesPageNum" :
2},
{
"SubBookMark" : [],
"Title" : "1.2" directory,
"DesPageNum" : 3
}
],
"Title" : "1" directory,
1
"desPageNum" :},
{
"SubBookMark" : [
{
"SubBookMark" : [
{
"SubBookMark" : [
],
"Title" : "directory 2.1.1,"
6 "desPageNum" :
},
{
"SubBookMark" : [
],
"Title" : "directory 2.1.2,"
"DesPageNum" : 7
}
],
"Title" : "2.1" directory,
"DesPageNum" : 5
},
{
"SubBookMark" : [],
"Title" : "2.2" directory,
11 "desPageNum" :
},
],
"Title" : "2" directory,
"DesPageNum" : 4
},
CodePudding user response:
Level on behalf of the hierarchyCodePudding user response:
Build a object and serializationCodePudding user response:
Create an entity class, corresponding to your data, then the type of serialization into a jsonCodePudding user response:
Need to download the Newtonsoft. Json. DLL, and in the project reference
Using Newtonsoft. Json;
using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
The namespace ConsoleApp6
{
Class Program
{
The static void Main (string [] args)
{
Var treeView=new treeView ()
{
Title="2" directory,
Level=1,
DesPageNum=4
};
Var childrenTree=new TreeChildrenView ()
{
Title="2.1" directory,
Level=2,
DesPageNum=5
};
Var childrenTree2=new TreeChildrenView ()
{
Title="2.2" directory,
Level=2,
DesPageNum=6
};
Var chchTree=new Tree2ChildrenView ()
{
2.1.1 "title=" catalogue,
Level=3,
DesPageNum=7
};
ChildrenTree. Childrens. Add (chchTree);
TreeView. Childrens. Add (childrenTree);
TreeView. Childrens. Add (childrenTree2);
String json=JsonConvert. SerializeObject (treeView);
Console. WriteLine (json);
The Console. ReadLine ();
}
}
///& lt; Summary>
The first major categories
//////& lt;/summary>
Public class TreeView
{
Public TreeView ()
{
This. Childrens=new List(a);
}
[JsonProperty (" title "))
Public string title {get; set; }
[JsonProperty (" Level ")]
Public int Level {get; set; }
[JsonProperty (" desPageNum ")]
Public int desPageNum {get; set; }
[JsonProperty (" children ")]
Public IListChildrens {get; set; }
}
///& lt; Summary>
///the first categories included in the second categories:
///& lt;/summary>
Public class TreeChildrenView
{
Public TreeChildrenView ()
{
This. Childrens=new List}
[JsonProperty (" title "))
Public string title {get; set; }
[JsonProperty (" Level ")]
Public int Level {get; set; }
[JsonProperty (" desPageNum ")]
Public int desPageNum {get; set; }
[JsonProperty (" children ")]
Public IList}
///& lt; Summary>
///the second categories include third category
///& lt;/summary>
Public class Tree2ChildrenView
{
[JsonProperty (" title "))
Public string title {get; set; }
[JsonProperty (" Level ")]
Public int Level {get; set; }
[JsonProperty (" desPageNum ")]
Public int desPageNum {get; set; }
}
}
CodePudding user response:
Only supports fixed 3 layers, such as provinces, cities and counties in this, if it is 4 layer, Tree2ChildrenView plus subclasses in layer 3, the layer structure whether meet the requirements of youThe