本文介绍了使用 JSON.NET 序列化/反序列化动态属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我有以下课程:
我想从名为 Type
的 JSON 字段中序列化/反序列化 Source
的值,例如:
I would like to serialize/deserialize the value for Source
from the JSON field named the value of Type
, for example:
或
两者都绑定到 Source
属性.
Where both bind to the Source
property.
推荐答案
你可以创建一个自定义 JsonConverter
处理动态属性名:
You could create a custom JsonConverter
to handle the dynamic property name:
要使用转换器,请向您的类添加 [JsonConverter]
属性,如下所示:
To use the converter, add a [JsonConverter]
attribute to your class like this:
这是一个有效的往返演示:https://dotnetfiddle.net/o7NDTV
Here is a working round-trip demo: https://dotnetfiddle.net/o7NDTV
这篇关于使用 JSON.NET 序列化/反序列化动态属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!