我有一个 .Net 对象,我一直在将其序列化为 Xml,并使用 Xml 属性进行装饰。我现在想将同一个对象序列化为 Json,最好使用 Newtonsoft Json.Net图书馆。
我想直接从内存中的 .Net 对象转到 Json 字符串(无需先序列化为 Xml)。我不希望向该类添加任何 Json 属性,而是希望 Json 序列化程序使用现有的 Xml 属性。
public class world{
[XmlIgnore]
public int ignoreMe{ get; }
[XmlElement("foo")]
public int bar{ get; }
[XmlElement("marco")]
public int polo{ get; }
}
成为
{
"foo":0,
"marco":0
}
最佳答案
使用 [JsonProperty(PropertyName="foo")]
属性并设置 PropertyName
。
关于c# - 将 .Net 对象序列化为 json,使用 xml 属性控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686817/