It is believed that friends who do. NET development often encounter the need for JSON serialization, and today the article concludes by using Servicestack.text to serialize JSON. It is much faster than Newtonsoft.json and found faster than Fastjson in the test.
First of all, we have the bottom two classes, one is the staff (Staff) class, one is the contact method (Contacts) class:
Copy Code code as follows:
public class Staff
{
Public long ID {get; set;}
public string Name {get; set;}
public int Age {get; set;}
}
public class contacts
{
Public long StaffID {get; set;}
public string Email {get; set;}
}
First, we add two employees:
Copy Code code as follows:
list<staff> Liststaff = new list<staff> ();
Liststaff.add (New Staff () {ID = 2, Name = "Xiao Li"});
Liststaff.add (New Staff () {ID = 3, Name = "Xiao Wang"});
By how many people used to serialize JSON:
Copy Code code as follows:
var result = "[";
foreach (var staff in Liststaff)
{
result = "{\" id\ ": \" "+ staff.id +" \ ", \" name\ ": \" "+ staff. Name + "\"}, ";
}
result = result. Substring (0, result. LENGTH-1);
result = = "]";
The final JSON is as follows:
Copy Code code as follows:
[
{
"ID": "2",
"Name": "Xiao Li"
},
{
"ID": "3",
"Name": "Xiao Wang"
}
]
This is possible, but there are several drawbacks: 1. The code appears cluttered and error prone. 2. You need to escape special characters, such as double quotes, otherwise JSON serialization will fail. Below look at using Servicestack.text to serialize JSON.
We need to download ServiceStack.Text.dll, reference it to our project, and reference the Servicestack.text namespace. Let's look at the serialization of a single Class object:
Copy Code code as follows:
Staff Staff = new Staff () {ID = 1, Name = "Xiaozhao"};
var result = staff. Tojson ();
This gets the JSON as follows:
Copy Code code as follows:
{
"ID": 1,
"Name": "Xiaozhao",
"Age": 0
}
Careful friends will find that we do not intend to use the age attribute in the output, but here is the output of age, in order to solve this problem, we have the bottom method.
1. Using the Jsonobject class, it inherits from Dictionary<string, String>, so that we can output the attributes we want like down.
Copy Code code as follows:
Staff Staff = new Staff () {ID = 1, Name = "Xiaozhao"};
Jsonobject json = new Jsonobject ();
Json. ADD ("ID", staff.id. ToString ());
Json. ADD ("Name", staff.) Name);
var result = json. Tojson ();
So get the result below [result one], achieve our goal.
Copy Code code as follows:
{
"ID": 1,
' Name ': ' Xiaozhao '
}
2. Add System.Runtime.Serialization to the project and reference the namespace System.Runtime.Serialization.
We can indicate the attributes to be serialized at the top of the attribute, as follows:
Copy Code code as follows:
[DataContract]
public class Staff
{
[DataMember]
Public long ID {get; set;}
[DataMember]
public string Name {get; set;}
public int Age {get; set;}
}
Or:
Copy Code code as follows:
public class Staff
{
Public long ID {get; set;}
public string Name {get; set;}
[Ignoredatamember]
public int Age {get; set;}
}
The result of the following code output becomes the same as [result one].
Copy Code code as follows:
Staff Staff = new Staff () {ID = 1, Name = "Xiaozhao"};
var result = staff. Tojson ();
Finally, we look at the serialization of the collection of class objects, and we add the staff contact form as follows:
Copy Code code as follows:
list<contact> listcontact = new list<contact> ();
Listcontact.add (New Contact () {StaffID = 3, Email = "xiaowang@163.com"});
Listcontact.add (New Contact () {StaffID = 4, Email = "xiaoli@163.com"});
Each employee may correspond to a contact method, here to consider the situation where the staff have no contact information, give the code directly:
Copy Code code as follows:
list<string> list = new list<string> ();
foreach (var staff in Liststaff)
{
Jsonobject json = new Jsonobject ();
Json. ADD ("ID", staff.id. ToString ());
Json. ADD ("Name", staff.) Name);
Contact Way
var contact = Listcontact.firstordefault (m => m.staffid = = staff.id);
if (contact!= null)
{
Jsonobject jsoncontact = new Jsonobject ();
Jsoncontact.add ("Email", contact. Email);
Notice here that the JSON string that was serialized with the contact object was added to the JSON object
Json. ADD ("Contacts", contact. Tojson ());
}
Serializing a JSON object and adding it to the list
List. ADD (JSON. Tojson ());
}
Get the final JSON string
var result = string. Format (' [{0}] ', String. Join (",", list));
Get the JSON:
Copy Code code as follows:
[
{
"ID": 2,
"Name": "Xiao Li"
},
{
"ID": 3,
"Name": "Xiao Wang",
"Contact": {
"StaffID": 3,
"Email": "Xiaowang@163.com"
}
}
]
In the case of deserialization, use Fromjson () to:
var staff = result. Fromjson<list<staff>> ();
This article briefly introduces the use of Servicestack.text to serialize JSON, hoping to help a friend who has not used it.
Author: Donqui