我有一个 .Net 对象,我已将其序列化为 Xml,并使用 Xml 属性进行装饰.我现在想将相同的对象序列化为 Json,最好使用 Newtonsoft Json.Net 库.
I have a .Net object which I've been serializing to Xml and is decorated with Xml attributes. I would now like to serialize the same object to Json, preferably using the Newtonsoft Json.Net library.
我想直接从内存中的 .Net 对象转到 Json 字符串(无需先序列化为 Xml).我不希望向类中添加任何 Json 属性,而是希望 Json 序列化程序使用现有的 Xml 属性.
I'd like to go directly from the .Net object in memory to a Json string (without serializing to Xml first). I do not wish to add any Json attributes to the class, but instead would like for the Json serializer to use the existing Xml attributes.
public class world{
[XmlIgnore]
public int ignoreMe{ get; }
[XmlElement("foo")]
public int bar{ get; }
[XmlElement("marco")]
public int polo{ get; }
}
变成
{
"foo":0,
"marco":0
}
原来这不是 Newtonsoft Json.Net 库的现有功能.我已经编写了一个补丁并将其上传到 Json.Net 问题跟踪器(存档链接 这里):
Turns out this wasn't an existing feature of the Newtonsoft Json.Net library. I've written a patch and uploaded it to the Json.Net issue tracker (archived link here):
这允许以下操作:
这篇关于将 .Net 对象序列化为 json,使用 xml 属性进行控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!