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