加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从动态对象生成JSON模式

发布时间:2020-12-15 22:03:51 所属栏目:百科 来源:网络整理
导读:我想从dynamic类型的对象中提取 JSON模式( as defined here). This is the best example I could find. 但是JSON.NET的Schema Generator需要查看实际的类/类型才能生成模式. 任何人对如何从动态对象中提取模式有任何想法? 解决方法 您仍然可以使用JSON.NET
我想从dynamic类型的对象中提取 JSON模式( as defined here).

This is the best example I could find.

但是JSON.NET的Schema Generator需要查看实际的类/类型才能生成模式.

任何人对如何从动态对象中提取模式有任何想法?

解决方法

您仍然可以使用JSON.NET从动态对象中提取JSON模式.你只需要一个动态类型的实际对象就可以做到这一点.请尝试以下示例:

dynamic person = new
            {
                Id = 1,FirstName = "John",LastName = "Doe"
            };

JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator {};

JsonSchema schema = schemaGenerator.Generate(person.GetType());

生成的JSON模式应如下所示:

{
    "type": "object","additionalProperties": false,"properties": {
        "Id": {
            "required": true,"type": "integer"
        },"FirstName": {
            "required": true,"type": [
                "string","null"
            ]
        },"LastName": {
            "required": true,"null"
            ]
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读