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:
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]}
JsonFx Supports several strategies of binding json to .net objects including dynamic objects.
https://github.com/jsonfx/jsonfx
I don't recall if Json.NET has support for dynamic objects yet, but it seems that you are able to do so with a little extra custom coding.
http://json.codeplex.com/ http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx
Microsoft has added a Json helper class to Web Matrix Beta 2. Sample code here http://www.mikesdotnetting.com/Article/160/WebMatrix-Working-With-The-JSON-Helper
There seems to be a privileges issue when using Json.Decode in a Console App. Right-mouse click on your EXE and "Run as administrator..." and it should work.
I am not sure if there is a way to force Visual Studio to run the executable as administrator for debugging purposes or not.
精彩评论