c# - 使用 JSON.NET 的序列化字段的顺序

标签 c# json json.net

有没有办法使用 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/

相关文章:

c++ - boost JSON 解析器在 MacOS 上转义引号,但在 Linux 上不转义

c# - 从异步方法返回IAsyncEnumerable

c# - 提交表单后从数据库中检索最新的主键 C#

c# - 这是什么系统颜色?

c# - 使用 JSON.NET 将已知和未知的 JSON 字段反序列化为 CLR 对象

c# - 当 json 中的 id 属性充当对象时创建 POCO

由 c# Json.NET ToJson() FromJson()

c# - 自定义类包含另一个自定义类时的 DataContractResolver/KnownType 问题

JsonPath:如果满足条件,则选择根级别字段

json - 如何在 Swift 中将 NSObject 类对象转换为 JSON?