官网
Json.Net用法很简单,本文也只是搬运了一下官网的例子,更多用法可以查看官网,一分钟就能上手。
https://www.newtonsoft.com/json
使用例子
对象序列化
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
string json = JsonConvert.SerializeObject(product);
// {
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
// }
反序列化
string json = @"{
'Name': 'Bad Boys',
'ReleaseDate': '1995-4-7T00:00:00',
'Genres': [
'Action',
'Comedy'
]
}";
Movie m = JsonConvert.DeserializeObject<Movie>(json);
string name = m.Name;
// Bad Boys
Linq to Json
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
JObject o = new JObject();
o["MyArray"] = array;
string json = o.ToString();
// {
// "MyArray": [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// }
安装方法
使用Nuget安装
Install-Package Newtonsoft.Json
About
了解更多有趣的操作请关注我的微信公众号:DealiAxy
每一篇文章都在我的博客有收录:blog.deali.cn
网友评论