采用序列化字段顺序JSON.NET [英] Order of serialized fields using JSON.NET

查看:81
本文介绍了采用序列化字段顺序JSON.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短而亲切:

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

这将足以指定单场始终出现第一位。


解决方案

我跟着 JsonConvert.SerializeObject(键)方法通过反射调用(其中key是一个IList)发现JsonSerializerInternalWriter.SerializeList被调用。它需要一个列表,并通过

遍历

的for(int i = 0; I< values​​.Count;我++){...

在这里值的IList中参数带来的。

简短的回答是......不,还有的方式来设置字段在JSON字符串中列出的顺序没有内置。

Short and sweet:

Is there a way to specify the order of fields in a serialized JSON object using JSON.NET?

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

解决方案

I followed the JsonConvert.SerializeObject(key) method call via reflection (where key was an IList) and found that JsonSerializerInternalWriter.SerializeList gets called. It takes a list and loops through via

for (int i = 0; i < values.Count; i++) { ...

where values is the IList parameter brought in.

Short answer is...No, there's no built in way to set the order the fields are listed in the JSON string.

这篇关于采用序列化字段顺序JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆