通过本文主要向大家介绍了json.net,json.net下载,c json.net,net.sf.json,net.sf.json jar包等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Json.net 常用使用小结(推荐)
using System;
using System.Linq;
using System.Collections.Generic;
namespace microstore
{
public interface IPerson
{
string FirstName
{
get;
set;
}
string LastName
{
get;
set;
}
DateTime BirthDate
{
get;
set;
}
}
public class Employee : IPerson
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public DateTime BirthDate
{
get;
set;
}
public string Department
{
get;
set;
}
public string JobTitle
{
get;
set;
}
}
public class PersonConverter : Newtonsoft.Json.Converters.CustomCreationConverter<IPerson>
{
//重写abstract class CustomCreationConverter<T>的Create方法
public override IPerson Create(Type objectType)
{
return new Employee();
}
}
public partial class testjson : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
// TestJson();
}
#region 序列化
public string TestJsonSerialize()
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
product.Price = 3.99M;
//product.Sizes = new string[] { "Small", "Medium", "Large" };
//string json = Newtonsoft.Json.JsonConvert.SerializeObject(product); //没有缩进输出
string json = Newtonsoft.Json.JsonConvert.SerializeObject(product, Newtonsoft.Json.Formatting.Indented);
//string json = Newtonsoft.Json.JsonConvert.SerializeObject(
// product,
// Newtonsoft.Json.Formatting.Indented,
// new Newtonsoft.Json.JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }
//);
return string.Format("<p>{0}</p>", json);
}
public string TestListJsonSerialize()
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
List<Product> plist = new List<Product>();
plist.Add(product);
plist.Add(product);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(plist, Newtonsoft.Json.Formatting.Indented);
return string.Format("<p>{0}</p>", json);
}
#endregion
#region 反序列化
public string TestJsonDeserialize()
{
string strjson = "{\"Name\":\"Apple\",\"Expiry\":\"2014-05-03 10:20:59\",\"Price\":3.99,\"Sizes\":[\"Small\",\"Medium\",\"Large\"]}";
Product p = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(strjson);
string template = @"<p><ul>
<li>{0}</li>
<li>{1}</li>
<li>{2}</li>
<li>{3}</li>
</ul></p>";
return string.Format(template, p.Name, p.Expiry, p.Price.ToString(), string.Join(",", p.Sizes));
}
public string TestListJsonDeserialize()
{
string strjson = "{\"Name\":\"Apple\",\"Expiry\":\"2014-05-03 10:20:59\",\"Price\":3.99,\"Sizes\":[\"Small\",\"Medium\",\"Large\"]}";
List<Product> plist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Product>>(string.Format("[{0},{1}]", strjson, strjson));
string template = @"<p><ul>
<li>{0}</li>
<li>{1}</li>
<li>{2}</li>
<li>{3}</li>
</ul></p>";
System.Text.StringBuilder strb = new System.Text.StringBuilder();
plist.ForEach(x =>
strb.AppendLine(
string.Format(template, x.Name, x.Expiry, x.Price.ToString(), string.Join(",", x.Sizes))
)
);
return strb.ToString();
}
#endregion
#region 自定义反序列化
public string TestListCustomDeserialize()
{
string strJson = "[ { \"FirstName\": \"Maurice\", \"LastName\": \"Moss\", \"BirthDate\": \"1981-03-08T00:00Z\", \"Department\": \"IT\", \"JobTitle\": \"Support\" }, { \"FirstName\": \"Jen\", \"LastName\": \"Barber\", \"BirthDate\": \"1985-12-10T00:00Z\", \"Department\": \"IT\", \"JobTitle\": \"Manager\" } ] ";
List<IPerson> people = Newtonsoft.Json.JsonConvert.DeserializeObject<List<IPerson>>(strJson, new PersonConverter());
IPerson person = people[0];
string template = @"<p><ul>
<li>当前List<IPerson>[x]对象类型:{0}</li>
<li>FirstName:{1}</li>
<li>LastName:{2}</li>
<li>BirthDate:{3}</li>
<li>Department:{4}</li>
<li>JobTitle:{5}</li>
</ul></p>";
System.Text.StringBuilder strb = new System.Text.StringBuilder();
people.ForEach(x =>
strb.AppendLine(
string.Format(
template,
person.GetType().ToString(),
x.FirstName,
x.LastName,
x.BirthDate.ToString(),
((Employee)x).Department,
((Employee)x).JobTitle
)
)
);
return strb.ToString();
}
#endregion
#region 反序列化成Dictionary
public string TestDeserialize2Dic()
{
//string json = @"{""key1"":""zhangsan"",""key2"":""lisi""}";
//string json = "{\"key1\":\"zhangsan\",\"key2\":\"lisi\"}";
string json = "{key1:\"zhangsan\",key2:\"lisi\"}";
Dictionary<string, string> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
string template = @"<li>key:{0},value:{1}</li>";
System.Text.StringBuilder strb = new System.Text.StringBuilder();
strb.Append("Dictionary<string, string>长度" + dic.Count.ToString() + "<ul>");
dic.AsQueryable().ToList().ForEach(x =>
{
strb.AppendLine(string.Format(template, x.Key, x.Value));
});
strb.Append("</ul>");
return strb.ToString();
}
#endregion
#region NullValueHandling特性
public class Movie
{
public string Name { get; set; }
public string Description { get; set; }
public string Classification { get; set; }
public string Studio { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<string> ReleaseCountries { get; set; }
}
/// <summary>
/// 完整序列化输出
/// </summary>
public string CommonSerialize()
{
Movie movie = new Movie();
movie.Name = "Bad Boys III";
movie.Description = "It's no Bad Boys";
string included = Newtonsoft.Json.JsonConvert.SerializeObject(
movie,
Newtonsoft.Json.Formatting.Indented, //缩进
new Newtonsoft.Json.JsonSerializerSettings { }
);
return included;
}
/// <summary>
/// 忽略空(Null)对象输出
/// </summary>
/// <returns></returns>
public string IgnoredSerialize()
{
Movie movie = new Movie();
movie.Name = "Bad Boys III";
movie.Description = "It's no Bad Boys";
string included = Newtonsoft.Json.JsonConvert.SerializeObject(
movie,

