Conversion between JSON and C # objects in Javascript in Asp.net

Source: Internet
Author: User

Method 1: splice JSON format in front-end JavaScript, array format (or set) [{},{}], object format {"":"","": ""} {Name :{"":""}}

Use List <t> listwebsituser = newtonsoft in the background. JSON. jsonconvert. deserializeobject (JSON, typeof (list <t>) as list <recommedwebsiteuser>; here, JSON is a set, so it is converted to a generic list <t>

Listwebsituser is a generic set of recommedwebsiteuser. In this way, the JSON string spliced at the front end is converted to a list of collections. <t>

[Serializable]

Class recommedwebsiteuser
{
Private string userid;

Public String userid
{
Get {return userid ;}
Set {userid = value ;}
}
Private string realname;

Public String realname
{
Get {return realname ;}
Set {realname = value ;}
}
}

To convert list <t> to JSON:

// The first method. This method requires reference of system. runtime. serialization. dll and system. servicemodel. Web. dll.
Datacontractjsonserializer serializer = new datacontractjsonserializer (listwebsituser. GetType ());

// Define a stream to store serialized content
Stream stream = new memorystream ();
Serializer. writeobject (stream, listwebsituser );

// Read stream data as a string from start to end and return
Stream. Position = 0;
Streamreader = new streamreader (Stream );
String json3 = streamreader. readtoend ();

// Method 2
String json1 = newtonsoft. JSON. jsonconvert. serializeobject (listwebsituser );

The two methods must mark the class as a serialized class.

Related Article

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.