I've been searching for a while for a nice and clean way to convert a JSON object to a dynamic object.
(I could cast to an object but the Twitter Streaming API actually sends two different JSON objects with the possibility of future object types!)
The code I use currently is from:
Deserialize JSON into C# dynamic object?
But its not the cleanest code and I was playing around with Web Matrix and noticed that they have a nice JSON.Decode(string) and JSON.Encode(object) methods and wanted to make use of them.
http://msdn.microsoft.com/en-us/library/system.web.helpers.json(v=vs.99).aspx
Adding a reference to System.Web.Helpers to my C# console application I managed to compile a solution calling JSON.Decode but... it throws a nasty exception.
This is probably down to me using it in a way not intended (outside Web Matrix) but any ideas? Probably expecting a simple, no thats silly answer ;-)
Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.
I'm using VS2010.
More detail: System.FieldAccessException was caught Message=Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed. Source=System.Web.Helpers StackTrace: at System.Web.Helpers.Json.Decode(String value) at Components.DataCollection.ConvertTwitterStream.ConvertTweets() in C:\Users\Administrator\documents\visual studio 2010\Projects\ISMM\Components\DataCollection\ConvertTwitterStream.cs:line 35 InnerException:
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Debugging calls to 'Json.Decode' fail when the Visual Studio hosting process is enabled (which is the default). I found it worked with the hosting process disabled or when running without the debugger.
The hosting process can be disabled for your project by following these instructions: http://msdn.microsoft.com/en-us/library/ms185330.aspx
To support jbtule's answer, JsonFx v2 (http://github.com/jsonfx/jsonfx) makes this really easy. The example below shows a full round-trip with dynamic object being built from a JSON string and then serialized back into JSON.
string input = "{ \"foo\": true, \"array\": [ 42, false, \"Hello!\", null ] }";
dynamic value = new JsonReader().Read(input);
// verify that it works
Console.WriteLine(value.foo); // true
Console.WriteLine(value.array[0]); // 42
Console.WriteLine(value.array.Length); // 4
string output = new JsonWriter().Write(value);
// verify that it works
Console.WriteLine(output); // {"foo":true,"array":[42,false,"Hello!",null]}
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