有没有办法使用JSON.NET指定序列化JSON对象中字段的顺序?

It would be sufficient to specify that a single field always appear first.

推荐答案

支持的方法是对要设置顺序的类属性使用JsonProperty属性.更多信息,请阅读JsonPropertyAttribute order documentation.

Pass the JsonProperty an Order value and the serializer will take care of the rest.

 [JsonProperty(Order = 1)]

This is very similar to the

 DataMember(Order = 1) 

在这System.Runtime.Serialization天里.

Here is an important note from @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

Json相关问答推荐

抓取低于w jolt的对象级别

如何使用GoFr返回XML响应?

使用JQ将JSON输出转换为CSV复杂 struct

JOLT转换以基于对象属性过滤JSON数组

XSLT 3.0 Json-to-xml,json 包含 html struct

(已回答)JSON 读取函数返回未定义而不是预期值 - Typescript

JOLT转换并向输出添加新数组

jq - 仅在键值对存在的地方打印值

如何修复通过在 tsconfig.json 文件中添加allowImportingTsExtensions引发的错误 TS5023?

N1QL 聚合查询 Couchbase

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

JBuilder中未定义的局部变量或方法json

JSON RPC - 什么是id?

我们可以使用 JSON 作为数据库吗?

Spring restTemplate 获取原始 json 字符串

Protocol Buffer vs Json - 何时 Select 一个而不是另一个

如何在dart Flutter 中将json字符串转换为json对象?

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?

Backbone.js 模型与集合