Joh*_*Con 30 c# serialization json
我有兴趣了解社区中的JSON库人员在.NET中使用过什么?我需要从.NET(C#)内部解析/序列化一些JSON对象图到实际的.NET类型.我可以自己动手,但是如果有人使用了一些实体库,我想听听你的意见.我在json.org网站上看到了图书馆列表,但它是一个相当大的列表,社区通常擅长审查伪装者的竞争者
您对图书馆体验的任何细节(优点/缺点)都将非常有用. - 提前致谢.
bri*_*nng 22
我过去曾经成功使用过Json.NET.
网站示例:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string json = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Run Code Online (Sandbox Code Playgroud)
Cha*_*ant 19
框架中至少有两个内置.
较新的:System.Runtime.Serialization.Json
和旧的:System.Web.Script.Serialization
我更喜欢不依赖第三方库.我每天都在使用JSON,并且从来没有比框架中已经存在的东西更多的东西.
ben*_*wey 10
我在程序集中使用DataContractJsonSerializer编写了我自己的JSON序列化System.ServiceModel.Web.dll程序[这是作为标准程序集在.NET 3.5中包含的WCF的一个组件,在.NET 3.5 SP1客户端配置文件中](在.NET 4.0和Silverlight 4中,它已被移动) to System.Runtime.Serialization.dll).
using System.IO;
using System.Runtime.Serialization.Json;
public class JsonObjectSerializer
{
public string Serialize<T>(T instance) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var memoryStream = new MemoryStream())
{
serializer.WriteObject(memoryStream, instance);
memoryStream.Flush();
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}
}
public T Deserialize<T>(string serialized) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var memoryStream = new MemoryStream())
{
using (var writer = new StreamWriter(memoryStream))
{
writer.Write(serialized);
writer.Flush();
memoryStream.Position = 0;
return serializer.ReadObject(memoryStream) as T;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22810 次 |
| 最近记录: |