Using json.net to generate JSON strings of object classes is very simple, for example:
// Entity class
Code
Public class sysnavmodel
{
Public sysnavmodel ()
{}
// /<Summary>
// /Constructor sys_nav
// /</Summary>
// /<Param name = "Navid"> Navid </param>
// /<Param name = "title"> title </param>
// /<Param name = "url"> URL </param>
// /<Param name = "iconcls"> iconcls </param>
// /<Param name = "parentid"> parentid </param>
// /<Param name = "visible"> visible </param>
// /<Param name = "orderid"> orderid </param>
Public sysnavmodel ( Int ? Navid, String title, string URL, string iconcls, Int ? Parentid, bool ? Visible, Int ? Orderid)
{
This . Navid = Navid;
This . Title = Title;
This . Url = URL;
This . Iconcls = Iconcls;
This . Parentid = Parentid;
This . Visible = Visible;
This . Orderid = Orderid;
}
# Region Object Attributes
Private Int ? _ Navid;
Private string _ title;
Private string _ URL;
Private string _ iconcls;
Private Int ? _ Parentid;
Private bool ? _ Visible;
Private Int ? _ Orderid;
Private string _ ptag;
// /<Summary>
// /Navid
// /</Summary>
Public Int ? Navid
{
Set {_ Navid = Value ;}
Get { Return _ Navid ;}
}
// /<Summary>
// /Page tag
// /</Summary>
Public String ptag
{
Get { Return _ Ptag ;}
Set {_ ptag = Value ;}
}
// /<Summary>
// /Navigation menu name
// /</Summary>
Public String title
{
Set {_ title = Value ;}
Get { Return _ Title ;}
}
// /<Summary>
// /Link address
// /</Summary>
Public String URL
{
Set {_ URL = Value ;}
Get { Return _ URL ;}
}
// /<Summary>
// /Icon Style Class Name
// /</Summary>
Public String iconcls
{
Set {_ iconcls = Value ;}
Get { Return _ Iconcls ;}
}
// /<Summary>
// /Superior ID
// /</Summary>
Public Int ? Parentid
{
Set {_ parentid = Value ;}
Get { Return _ Parentid ;}
}
// /<Summary>
// /Show or not
// /</Summary>
Public bool ? Visible
{
Set {_ visible = Value ;}
Get { Return _ Visible ;}
}
// /<Summary>
// /Sort ID
// /</Summary>
Public Int ? Orderid
{
Set {_ orderid = Value ;}
Get { Return _ Orderid ;}
}
// /<Summary>
// /Permission Array
// /</Summary>
Public arraylist Permissions
{
Get
{
Return Navpermissiondal. Current. getpermissionarray ( This . Navid. value );
}
}
# Endregion
// MainCode
Public override string tostring ()
{
Stringbuilder sb = New Stringbuilder ();
Stringwriter SW = New Stringwriter (SB );
NewJsonserializer (). serialize (NewJsontextwriter (SW ),This);
ReturnSB. tostring ();
}}
When calling the tostring () method of the object class, you can directly call the tostring () method of the object class to generate a JSON string. Easy to use
We can encapsulate a method for future use.
Public static string tojson (Object OBJ)
{
Stringbuilder sb = New Stringbuilder ();
Stringwriter SW = New Stringwriter (SB );
Jsonserializer JSON= NewJsonserializer ();
JSON. serialize (NewJsontextwriter (SW), OBJ );
ReturnSB. tostring ();
}