How do I convert a string of json formatted data into an anonymous object?
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
You create anonymous types by using the new operator together with an object initializer. For more information about object initializers, see Object and Collection Initializers. The following example shows an anonymous type that is initialized with two properties named Amount and Message .
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
C# 4.0 adds dynamic objects that can be used. Have a look at this.
using dynamics is something like this:
string jsonString = "{\"dateStamp\":\"2010/01/01\", \"Message\": \"hello\" }";
dynamic myObject = JsonConvert.DeserializeObject<dynamic>(jsonString);
DateTime dateStamp = Convert.ToDateTime(myObject.dateStamp);
string Message = myObject.Message;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With