Serializing JSON string to ExpandoObject

In some case you want to create and Object from JSON, a great way of doing this to deserialize this JSON to an ExpandoObject.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
/// <summary>
/// Convert Json String to an Expando Object
/// </summary>
/// <param name="that">Json string to convert</param>
/// <returns>Expando Object</returns>
public static ExpandoObject ToExpando(this string that)
{
    ExpandoObject result;
    try
       {
         result = JsonConvert.DeserializeObject<ExpandoObject>(that, new ExpandoObjectConverter());
     }
     catch (Exception e)
     {
        throw e;
     }
     return result;
}

Here we use JsonConvert.DeserializeObject, you need to add reference to NewtonSoft.Json see below.

1
2
3
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

Comments

Popular posts from this blog

Automatically Discover and Assign Parameter with Values to a Stored Procedure Call in C#