c-sharp

Convert json string to object in C# .Net

using System.Web.Script.Serialization;

public class JsonSerialOperations {
    public void DeserailizeJsonString() {
        var jsonString = "{\"firstname\": \"hello\"}";

        var json_serializer = new JavaScriptSerializer();
        var data = (IDictionary<string, object>)json_serializer.DeserializeObject(jsonString);

        Console.WriteLine(data["firstname"]);
    }
}
Output
hello

You can use JavaScriptSerializer to convert json string to C# object. You will have to import using System.Web.Script.Serialization; to use Javascript Serializer.

The code snippet return a c# dictionary from the json string and now you can get its values by using its key name. We are getting firstname key in the code snippet example.

Was this helpful?