如何将JObject反序列化到.NET对象

方式一

  1. class Person {
  2. public string Name { get; internal set; }
  3. public DateTime BirthDate { get; internal set; }
  4. }
  5. JObject o = new JObject(
  6. new JProperty("Name", "John Smith"),
  7. new JProperty("BirthDate", new DateTime(1983, 3, 20))
  8. );
  9. JsonSerializer serializer = new JsonSerializer();
  10. Person p = (Person)serializer.Deserialize(new JTokenReader(o), typeof(Person));
  11. Console.WriteLine(p.Name);

方式二

  1. // pick out one album
  2. JObject jalbum = albums[0] as JObject;
  3. // Copy to a static Album instance
  4. Album album = jalbum.ToObject<Album>();