JSON.NET C# 类库

下载地址:

http://james.newtonking.com/ 

 

示例:

 

public class Message
{
  public string Address { get; set; }
 
  [JsonProperty(TypeNameHandling = TypeNameHandling.All)]
  public object Body { get; set; }
}
 
public class SearchDetails
{
  public string Query { get; set; }
  public string Language { get; set; }
}

 

 

 

Message message = new Message();
message.Address = "http://www.google.com";
message.Body = new SearchDetails
  {
    Query = "Json.NET",
    Language = "en-us"
  };
 
string json = JsonConvert.SerializeObject(message, Formatting.Indented);
// {
//   "Address": "http://www.google.com",
//   "Body": {
//     "$type": "Newtonsoft.Json.Tests.Serialization.SearchDetails, Newtonsoft.Json.Tests",
//     "Query": "Json.NET",
//     "Language": "en-us"
//   }
// }
 
Message deserialized = JsonConvert.DeserializeObject<Message>(json);
 
SearchDetails searchDetails = (SearchDetails) deserialized.Body;
// Json.NET

 

 

posted @ 2011-11-08 16:35  PPDev  阅读(6872)  评论(0编辑  收藏  举报