I have the following code:
var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
The input in responsecontent
is JSON, but it is not properly parsed into an object. How should I properly deserialize it?
I am assuming you are not using Json.NET (Newtonsoft.Json NuGet package). If this the case, then you should try it.
It has the following features:
JsonIgnore
and JsonProperty
can be added to a class to customize how a class is serializedLook at the example below. In this example, JsonConvert
class is used to convert an object to and from JSON. It has two static methods for this purpose. They are SerializeObject(Object obj)
and DeserializeObject<T>(String json)
:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string json = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);