Use json.net to generate a JSON string for the object class

Source: Internet
Author: User

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 ();
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.