C # JSON string deserialization

Source: Internet
Author: User

Usage scenario: JS in development passes a JSON-formatted string into the background, and the background converts the JSON string into a list collection

1  Public Static classJSON2     {3          Public StaticList<t> jsonstringtolist<t> ( This stringjsonstr)4         {5JavaScriptSerializer serializer =NewJavaScriptSerializer ();6list<t> OBJS = serializer.deserialize<list<t>>(JSONSTR);7             returnObjs;8         }9 Ten          Public StaticT deserialize<t> (stringJSON) One         { AT obj = activator.createinstance<t>(); -             using(MemoryStream ms =NewMemoryStream (Encoding.UTF8.GetBytes (JSON))) -             { theDataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ()); -                 return(T) serializer. ReadObject (MS); -             } -         } +}

Encapsulates a JSON static class, which defines two static methods, which are only required to invoke the first method when using JSON deserialization: JSON. Jsonstringtolist<t> (String jsonstring)

T: Type jsonstring: As JSON string

This code will add several references:

1 using System.Web.Script.Serialization; 2 using System.IO; 3 using System.Text; 4 using System.Runtime.Serialization.Json;

C # JSON string deserialization

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.