首页 > 解决方案 > C# 将我的 JsonSchema 的属性转换为 Json Schema 并动态添加新字段

问题描述

我用一些标准属性创建了自己的 JsonSchema。

    public class JsonSchema
{
    public SchemaType Type { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Const { get; set; }
    public IList<Dictionary<string, object>> Properties { get; set; }
    public IList<object> Enum { get; set; }
    public IList<JsonSchema> Items { get; set; }
    public IList<string> Required { get; set; }
    public IList<JsonSchema> AllOf { get; set; }
    public IList<JsonSchema> AnyOf { get; set; }
    public IList<JsonSchema> OneOf { get; set; }
}

我需要将其转换为 Json Schema 字符串。

我正在使用 JsonConvert.SerializeObject 对其进行转换。几乎一切正常,但属性“属性”正在转换为 JsonSchema 数组,如下所示:

    {
      "type": "object",
      "properties": [
        {
          "f_1022": {
            "type": "string",
            "title": "Zona",
            "oneOf": [
              {
                "title": "Urbana",
                "description": "Urbana",
                "const": "o_1043"
              },
              {
                "title": "Rural",
                "description": "Rural",
                "const": "o_1044"
              },
              {
                "title": "Periurbana",
                "description": "Periurbana",
                "const": "o_1045"
              }
            ]
          }
        },
        {
          "f_1023": {
            "type": "string",
            "title": "Etnia/Cor"
          }
        },
      ]
    }

我需要结果是这样的:

    {
      "type": "object",
      "properties": 
      {
          "f_1022": {
            "type": "string",
            "title": "Zona",
            "oneOf": [
              {
                "title": "Urbana",
                "description": "Urbana",
                "const": "o_1043"
              },
              {
                "title": "Rural",
                "description": "Rural",
                "const": "o_1044"
              },
              {
                "title": "Periurbana",
                "description": "Periurbana",
                "const": "o_1045"
              }
            ]
          },
          "f_1023": {
            "type": "string",
            "title": "Etnia/Cor"
          }
    }
}

请注意,最后一个 Json 属性没有括号。

如何让 JsonConvert 像最后一个 Json Schema 一样转换?

谢谢

标签: c#jsonjsonschema

解决方案


推荐阅读