Json Api的参考
https://www.newtonsoft.com/json
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Test
{
public static void Main()
{
Product product = new Product();
product.ExpiryDate = new DateTime(2008, 12, 28);
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
using (StreamWriter sw = new StreamWriter(@"D:\json.txt"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, product);
// {"ExpiryDate":new Date(1230375600000),"Price":0}
// without the converters
// {"ExpiryDate":"2008-12-28T00:00:00"}
}
using (StreamReader sr = new StreamReader(@"D:\json.txt"))
using (JsonReader reader = new JsonTextReader(sr))
{
var item = serializer.Deserialize<Product>(reader);
//这里使用泛型方法是必要的,不然
//Product? p = item as Product;
if (item!= null)
{
Console.WriteLine(item.ExpiryDate);
}
}
}
}
C# 使用Newtonsoft.Json处理JSON
这篇博客介绍了如何在C#中使用Newtonsoft.Json库来解析和序列化JSON数据,包括JsonApi的使用和最佳实践。
294

被折叠的 条评论
为什么被折叠?



