c# - C# json 自定义序列化
问题描述
我在 c# 中创建了一个自定义标签对象,我需要从这个对象创建 json 对象。但是我已经将标签从“标签”控件派生到自定义标签对象。序列化自定义标签对象后,json 被标签属性填充。但是,我不需要它。我只需要传递自定义标签对象。
这是自定义标签:
public class customLabel:Label
{
public string X { get; set; }
public string Y { get; set; }
public string H { get; set; }
public string W { get; set; }
public string FontName { get; set; }
public string FontSize { get; set; }
public string Type { get; set; }
public string Align { get; set; }
public string _Text { get; set; }
}
我正在使用 Newtonsoft.Json
json 序列化
解决方案
创建包含所需属性的自定义。JsonConvertor
然后将其传递给以SerializeObject
控制序列化。
string result = JsonConvert.SerializeObject(
customLabel,
Formatting.Indented,
new CustomLabelConverter(typeof(CustomLabel)));
推荐阅读
- windows - windows Jenkins上的groovy命令curl
- hyperledger-fabric - 在 Hyperledger Fabric 中,客户端是向所有订单发送交易还是仅向一个订购者节点发送交易?
- c# - 无法将 int 类型隐式转换为 (namespace.model.class) 类型
- python - 如何用另一个数据框中最接近的日期填充一个数据框中的日期列
- google-sheets - 提取根域以及 Google 表格中的 TLD regexextract
- model-binding - 在模型绑定上处理 Nullable 引用和 RequiredAttribute 的正确方法
- c++ - 使用 Qt Creator 在调试模式下编译一段代码
- html - 自动调整和对齐 td
- python - 带有python脚本的pdf-latex,自定义python变量到latex输出
- c# - Selenium 中与 WebElement 交互的最佳等待策略是什么