且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Web Matrix的助手C#JSON解码

更新时间:2023-02-17 20:25:30

要支持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]}