转载:http://www.rainsts.net/article.asp?id=817
JSON 应用愈发广泛,相对 XML 而言,JSON 格式的数据量更小,更利于传输和存储。在 Ajax、Data Service、Silverlight 等框架中都有大量应用。但 .NET Framework 对 JSON 的支持还很弱,由于无法使用动态类型,因此 JavaScriptSerializer().DeserializeObject() 只是简单地将 "未知类型" 的目标反序列化为一个泛型词典(Dictionary),看上去像个半成品。
CodePlex Json.NET 是另外一种选择,作者特意提供了一个 JsonConvert.DeserializeAnonymousType(),使得我们可以用一个匿名对象实例来指定反序列化类型。
var o = new
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } }
};
var json = JsonConvert.SerializeObject(o);
var anonymous = new { a = 0, b = String.Empty, c = new int[0], d = new Dictionary<string, int>() };
var o2 = JsonConvert.DeserializeAnonymousType(json, anonymous);
Console.WriteLine(o2.b);
Console.WriteLine(o2.c[1]);
var o3 = JsonConvert.DeserializeAnonymousType(json, new { c = new int[0], d = new Dictionary<string, int>() });
Console.WriteLine(o3.d["y"]);
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } }
};
var json = JsonConvert.SerializeObject(o);
var anonymous = new { a = 0, b = String.Empty, c = new int[0], d = new Dictionary<string, int>() };
var o2 = JsonConvert.DeserializeAnonymousType(json, anonymous);
Console.WriteLine(o2.b);
Console.WriteLine(o2.c[1]);
var o3 = JsonConvert.DeserializeAnonymousType(json, new { c = new int[0], d = new Dictionary<string, int>() });
Console.WriteLine(o3.d["y"]);
DeserializeAnonymousType 只是借助这个匿名对象参数(anonymous) 反射类型而已,也就是说它和反序列化结果并非同一个对象。正如 o3 那样,我们也可以只提取局部信息。
实际上,我们也可以直接反序列化为 JObject,然后通过索引器直接访问。JObject、JProperty 等都继承自 JToken,它重载了基元类型转换操作符,我们可以直接得到实际结果。
var o = new
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } }
};
var json = JsonConvert.SerializeObject(o);
var o2 = JsonConvert.DeserializeObject(json) as JObject;
Console.WriteLine((int)o2["a"]);
Console.WriteLine((string)o2["b"]);
Console.WriteLine(o2["c"].Values().Count());
Console.WriteLine((int)o2["d"]["y"]);
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } }
};
var json = JsonConvert.SerializeObject(o);
var o2 = JsonConvert.DeserializeObject(json) as JObject;
Console.WriteLine((int)o2["a"]);
Console.WriteLine((string)o2["b"]);
Console.WriteLine(o2["c"].Values().Count());
Console.WriteLine((int)o2["d"]["y"]);
Json.NET 同样支持强类型特性标注和反序列化,同时还提供了几个常用的格式转换器。
[JsonObject]
class MyClass
{
[JsonProperty]
public int a { get; set; }
[JsonProperty]
public string b { get; set; }
[JsonProperty]
public int[] c { get; set; }
[JsonProperty]
public Dictionary<string, int> d { get; set; }
[JsonIgnore]
public float e { get; set; }
[JsonProperty]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime f { get; set; }
}
class Program
{
static void Main(string[] args)
{
var o = new MyClass
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } },
f = DateTime.Now
};
var json = JsonConvert.SerializeObject(o);
var o2 = JsonConvert.DeserializeObject<MyClass>(json);
}
}
class MyClass
{
[JsonProperty]
public int a { get; set; }
[JsonProperty]
public string b { get; set; }
[JsonProperty]
public int[] c { get; set; }
[JsonProperty]
public Dictionary<string, int> d { get; set; }
[JsonIgnore]
public float e { get; set; }
[JsonProperty]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime f { get; set; }
}
class Program
{
static void Main(string[] args)
{
var o = new MyClass
{
a = 1,
b = "Hello, World!",
c = new[] { 1, 2, 3 },
d = new Dictionary<string, int> { { "x", 1 }, { "y", 2 } },
f = DateTime.Now
};
var json = JsonConvert.SerializeObject(o);
var o2 = JsonConvert.DeserializeObject<MyClass>(json);
}
}
LINQ to JSON 看上去有点像 LINQ to XML,使用非常简单,有关详情可参考帮助文件。