DataContract with Json.Net

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataContract with Json.Net相关的知识,希望对你有一定的参考价值。

https://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm

如果class使用了DataContract,name没有使用DataMember的property就不解析

[DataContract]
public class File
{
    // excluded from serialization
    // does not have DataMemberAttribute
    public Guid Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Size { get; set; }
}

File file = new File
{
    Id = Guid.NewGuid(),
    Name = "ImportantLegalDocuments.docx",
    Size = 50 * 1024
};

string json = JsonConvert.SerializeObject(file, Formatting.Indented);

Console.WriteLine(json);
// {
//   "Name": "ImportantLegalDocuments.docx",
//   "Size": 51200
// }

 

以上是关于DataContract with Json.Net的主要内容,如果未能解决你的问题,请参考以下文章

DataContract,默认 DataMember 值

包含异常的 DataContract

从 XSD 生成 DataContract

为啥即使服务仍然引用它,也会在 DataContract 上调用 Dispose?

WCF -Rest- DataContract:反序列化 XML 包装的响应

数据契约[DataContract]