使用Json.net - 一个C#对象的局部自定义序列 [英] Using Json.net - partial custom serialization of a c# object
问题描述
我是个使用NewtonsoftsJson.Net序列化一些和对象JSON的数组。
中的对象具有一组共同属性的,但是也有元属性是一个辞典
I ma using Newtonsofts' Json.Net to serialize some and array of objects to json. The objects have a common set of properties but also have Meta property which is a dictionary
在序列我想要的关键值对被添加到我的JSON因为如果他们其中root级别的属性,这样的对象...
During serialization I want the key value pairs to be added to my json object as if they where root level properties, like this...
{
id: 1,
name:'jeff',
food:'spinch',
spoon: 'ýes'
}
不喜欢这样的:
{
id: 1,
name:'jeff',
meta:{
food:'spinch',
spoon: 'ýes'
}
}
我已经通过JsonSerializerSettings挖,但不能似乎发现,我可以跳,并覆盖???
I have dug through JsonSerializerSettings but cant seem to spot where I can jump in and override???
推荐答案
您可以通过创建做到这一点你自己的 JsonConverter
,然后添加到您想要的类属性序列
[JsonConverter(typeof运算(MyConverter))]
You can do this by creating your own JsonConverter
and then adding an attribute to the class you want to serialize
[JsonConverter(typeof(MyConverter))]
例如,在这里 -
< A HREF =http://www.lostechies.com/blogs/rhouston/archive/2008/02/25/a-custom-converter-for-json-net.aspx相对=nofollow> HTTP:// www.lostechies.com/blogs/rhouston/archive/2008/02/25/a-custom-converter-for-json-net.aspx
Example here - http://www.lostechies.com/blogs/rhouston/archive/2008/02/25/a-custom-converter-for-json-net.aspx
这篇关于使用Json.net - 一个C#对象的局部自定义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!