C# 指定一个值可以是带有 json 架构的字符串或空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16241333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
specify a value can be a string or null with json schema
提问by evanmcdonnal
Hopefully this isn't obvious to others because I find the docs at http://json-schema.org/to be lacking in finer details. I'm getting a block of json with some properties that can be null or a string. How do you specify, in a json schema (to be parsed by json.NET's JsonSchema.Parsemethod), that a value can be of type null or type string?
希望这对其他人来说并不明显,因为我发现http://json-schema.org/上的文档缺乏更详细的细节。我得到了一个 json 块,其中包含一些可以为 null 或字符串的属性。您如何在 json 模式(由 json.NET 的JsonSchema.Parse方法解析)中指定值可以是 null 类型或字符串类型?
Is there something simple I'm missing like supplying an array for the type? For example;
是否有一些简单的东西我遗漏了,比如为该类型提供一个数组?例如;
"member_region": { "type": [ "string", null ] } // this throws an exception
Also, does anyone have a better source for json schema details then the json-schema.org? Where can I find a larger selection of examples? I don't want to read a big doc/spec to find something that can easily be demonstrated in a 10 line example.
另外,有没有人有比 json-schema.org 更好的 json 模式详细信息来源?在哪里可以找到更多示例?我不想阅读大文档/规范来找到可以在 10 行示例中轻松演示的内容。
采纳答案by Explosion Pills
From http://json-schema.org/latest/json-schema-validation.html#anchor79
来自http://json-schema.org/latest/json-schema-validation.html#anchor79
The value of this keyword MUST be either a string or an array. If it is an array, elements of the array MUST be strings and MUST be unique.
String values MUST be one of the seven primitive types defined by the core specification.
此关键字的值必须是字符串或数组。如果它是一个数组,则该数组的元素必须是字符串并且必须是唯一的。
字符串值必须是核心规范定义的七种基本类型之一。
Then we refer to types: http://json-schema.org/latest/json-schema-core.html#anchor8
然后我们参考类型:http: //json-schema.org/latest/json-schema-core.html#anchor8
It lists string and null. Try:
它列出了字符串和空值。尝试:
"member_region": { "type": "string, null" }
回答by zardilior
Extending on Explosion Pills answer if you go for the array syntax:
如果您使用数组语法,则扩展爆炸药丸答案:
"member_region": { "type": [ "string", "null" ] } // this works
because you are stating a type not an example you shouldn't go for:
因为你说的是一个类型而不是一个你不应该去的例子:
"member_region": { "type": [ "string", null ] } // this throws an exception

