String - DeserializeJavaScript
A string extension method that converts a JSON-formatted string to an object of the specified type.
public static void Main() { var myJsString = "[\"Fizz\",\"Buzz\"]"; // C# Extension Method: String - DeserializeJavaScript var result = myJsString.DeserializeJavaScript<List<string>>(); foreach(var val in result) { Console.WriteLine(val); } }
View Source
#if !NETSTANDARD using System.Web.Script.Serialization; #endif public static partial class Extensions { #if !NETSTANDARD /// <summary> /// A string extension method that deserialize a string binary as <T>. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>The desrialize binary as <T></returns> public static T DeserializeJavaScript<T>(this string @this) { var serializer = new JavaScriptSerializer(); return serializer.Deserialize<T>(@this); } #endif }