有没有办法使用 JSON.NET 来指定序列化 JSON 对象中的字段顺序? ?
指定一个字段总是首先出现就足够了。
最佳答案
支持的方法是在要为其设置顺序的类属性上使用 JsonProperty
属性。阅读 JsonPropertyAttribute order documentation了解更多信息。
向 JsonProperty
传递一个 Order
值,序列化程序将处理其余的工作。
[JsonProperty(Order = 1)]
这个很像
DataMember(Order = 1)
System.Runtime.Serialization
天。
这是来自@kevin-babcock 的重要说明
... setting the order to 1 will only work if you set an order greater than 1 on all other properties. By default any property without an Order setting will be given an order of -1. So you must either give all serialized properties and order, or set your first item to -2
关于c# - 使用 JSON.NET 的序列化字段的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3330989/