Home > Net >  Evaluate a json method
Evaluate a json method

Time:09-25

Have a flat level directory, want to switch to more obvious hierarchy json
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 hierarchy

CodePudding user response:

Build a object and serialization

CodePudding user response:

Create an entity class, corresponding to your data, then the type of serialization into a json

CodePudding 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 IList Childrens {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 you

The
refer to the original poster qiujg response:

CodePudding user response:

 public static String SerializeToJsonString (Object data) 
{
Using (MemoryStream ms=new MemoryStream ())
{
Var dataContractSerializer=new DataContractJsonSerializer (data. GetType ());
DataContractSerializer. WriteObject (ms, data);
Byte [] json=Ms. ToArray ();
Return. Encoding UTF8. Get string (json, 0, json. Length);
}
}

Public static T DeserializeFromJsonString (string json)
{
Using (MemoryStream ms=new MemoryStream (Encoding UTF8. GetBytes (json)))
{
DataContractJsonSerializer ser=new DataContractJsonSerializer (typeof (T));
Return (T) ser. ReadObject (ms).
}
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related