使用Web Matrix的助手C#JSON解码 [英] C# JSON decoding using Web Matrix helpers
问题描述
我一直在寻找了一段时间的一个非常干净的方式JSON对象转换为动态对象。
I've been searching for a while for a nice and clean way to convert a JSON object to a dynamic object.
(我可以转换为一个对象,但Twitter的流API实际发送两个不同的JSON对象未来的对象类型的可能性)
(I could cast to an object but the Twitter Streaming API actually sends two different JSON objects with the possibility of future object types!)
我目前使用的代码是:
但它不是最干净的代码,我用Web Matrix的玩耍,发现他们有一个很好的JSON.Decode(String)和JSON.Encode(对象)方法并希望利用它们。
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
添加到System.Web.Helpers我的C#控制台应用程序的引用我设法编译一个解决方案,呼吁JSON.Decode但是......它抛出一个讨厌的例外。
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.
这可能是下到我使用它的方式不适合(Web Matrix的外),但任何想法?也许期待一个简单的,这就是不傻的答案; - )
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 ;-)
由方法尝试System.Web.Helpers.Json.Decode(System.String)'来场 System.Web.Helpers.Json._serializer失败。
Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.
我使用VS2010。
更多细节:
System.FieldAccessException被抓住
消息=由法'System.Web.Helpers.Json.Decode(System.String)访问现场尝试 System.Web.Helpers.Json._serializer失败。
来源= System.Web.Helpers
堆栈跟踪:
在System.Web.Helpers.Json.Decode(字符串值)Components.DataCollection.ConvertTwitterStream.ConvertTweets
()的C:\Users\Administrator\documents\visual工作室2010\Projects\ISMM\Components\DataCollection\ConvertTwitterStream.cs:行35
的InnerException:
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:
推荐答案
要支持jbtule的回答,JsonFx V2( http://github.com / jsonfx / jsonfx )使这很容易。下面的例子显示了一个完整的往返与动态对象从JSON字符串正在兴建,然后序列化回JSON。
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]}
这篇关于使用Web Matrix的助手C#JSON解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!