没有合适的资源?快使用搜索试试~ 我知道了~
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
资源详情
资源评论
资源推荐
Json.net 常用使用小结常用使用小结(推荐推荐)
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
剩余14页未读,继续阅读
weixin_38695773
- 粉丝: 10
- 资源: 956
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0