c# - 使用 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

https://stackoverflow.com/questions/3330989/

相关文章:

c# - 如何在 C# 中将字典转换为 JSON 字符串?

c# - JSON.net:如何在不使用默认构造函数的情况下反序列化?

json - REST API - 文件(即图像)处理 - 最佳实践

ios - 如何在 Swift 中解析来自 Alamofire API 的 JSON 响应?

android - 如何使用 Android 通过 Request 发送 JSON 对象?

arrays - YAML 等效于 JSON 中的对象数组

javascript - 如何动态创建 JavaScript 数组(JSON 格式)?

javascript - 从浏览器下载 JSON 对象作为文件

c# - 在 JSON.NET 中反序列化的转换接口(interface)

mysql - 在 MySQL 中以 JSON 格式存储数据