json 模式如何表示具有不同对象的数组?
How can a json schema represent an array with different objects?
我熟悉 json 单一对象类型数组的模式。
例如。对于此 json 示例:
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 1,
"Steps": {
"steps": 4000
}
}
]
}
...相关的架构格式(我已经介绍过)可能是:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
}
}
}
}
}
}
但是我如何调整此架构以允许不同类型的对象,例如
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 2,
"HeartRate": {
"heartrates": 4000
}
}
]
}
?
例子改编自
我希望架构验证在 https://www.jsonschemavalidator.net/
时通过
我不明白为什么,但是上面的第二个例子使用上面的模式在提到的在线 json 模式验证器中通过了。不管它为什么通过,我都不认为模式是正确的,因为它并不代表可以有“心率”对象。
我确实读过 JSON Schema - array of different objects,但我相信这不是骗局,因为该问答是关于“Swagger 2.0”文档的,我没有使用它。
更新:
这会是 reasonable/correct 架构吗?
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}
第二个模式是否正确识别“arr
是一个对象数组,其中包含一个名为 type
的整数,或者一个名为 Steps
或名为 Heartrate
"?
的对象
好吧,它通过了,因为从技术上讲它是有效的,您可以在数组中包含对象的任意组合。该模式明确指出可以有一个心率对象。您说您熟悉单一对象类型的数组,但您应该知道数组不关心其中包含什么类型的对象,这就是为什么您引入的模式实际上是有效的。它是否适合您要实现的功能是另一个问题。
这意味着当您需要拉取一个对象时,您将需要一个非常复杂的循环来遍历数组,假设对象太多以至于您无法对它们进行充分索引,或者您需要某种排序索引和数组中对象的地址,这可能写起来很麻烦但可行。所以问题是,你打算用这些对象做什么?从您使用的属性来看似乎很明显,看起来像是某种健身监测器,但是您打算如何处理数据并不明显,因此即使此模式有效也很难说出您可以做什么,请记住,它确实是数组的有效结构。
虽然如果您确实有两种类型的对象,有人可能会问为什么不简单地为每种类型创建一个数组,该数组将具有特定于其内容的支持数据拉取功能。但它会再次询问您打算做什么。
希望这对您的思考过程有所帮助。
更新...
不知道你打算做什么,这里是我建议你使用的数据结构格式。
let someArray = [
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
},
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
}
]
你的例子似乎有一些冗余,他们明确指出某物是一个对象,某物是该对象的 属性。 JavaScript 已经知道所有这些,因此无需为您创建的函数添加更多复杂性来使用这些函数,然后必须深入所有这些层。我建议你创建一个对象,每个对象都有一个你声明的类型的 属性 对象,如果你想根据需要扩展或压缩该对象。这将大大降低您的结构的复杂性。因此您会看到您有一个对象数组,这些对象包含表示您打算收集的数据的属性。
但这假设您收集的数据来自多个来源。再次需要更多地了解您要完成的工作。
Does the second schema correctly identify that "arr is an array of objects that have an integer named type an integer, and either an object named Steps or an object named Heartrate"?
是的,有点。您没有说明哪些属性 必须 存在,哪些属性不能同时存在,我们可以使用“required”和“oneOf”关键字来做到这一点。
这是更新后的架构,还修复了您在“心率”部分中犯的错误(您说的是“步数”而不是“心率”):
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"required": [ "type" ],
"oneOf": [
{ "required": ["Steps"] },
{ "required": ["HeartRate"] }
},
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"required": ["steps"],
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"required": ["heartrates"],
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}
我熟悉 json 单一对象类型数组的模式。 例如。对于此 json 示例:
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 1,
"Steps": {
"steps": 4000
}
}
]
}
...相关的架构格式(我已经介绍过)可能是:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
}
}
}
}
}
}
但是我如何调整此架构以允许不同类型的对象,例如
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 2,
"HeartRate": {
"heartrates": 4000
}
}
]
}
?
例子改编自
我希望架构验证在 https://www.jsonschemavalidator.net/
时通过我不明白为什么,但是上面的第二个例子使用上面的模式在提到的在线 json 模式验证器中通过了。不管它为什么通过,我都不认为模式是正确的,因为它并不代表可以有“心率”对象。
我确实读过 JSON Schema - array of different objects,但我相信这不是骗局,因为该问答是关于“Swagger 2.0”文档的,我没有使用它。
更新:
这会是 reasonable/correct 架构吗?
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}
第二个模式是否正确识别“arr
是一个对象数组,其中包含一个名为 type
的整数,或者一个名为 Steps
或名为 Heartrate
"?
好吧,它通过了,因为从技术上讲它是有效的,您可以在数组中包含对象的任意组合。该模式明确指出可以有一个心率对象。您说您熟悉单一对象类型的数组,但您应该知道数组不关心其中包含什么类型的对象,这就是为什么您引入的模式实际上是有效的。它是否适合您要实现的功能是另一个问题。
这意味着当您需要拉取一个对象时,您将需要一个非常复杂的循环来遍历数组,假设对象太多以至于您无法对它们进行充分索引,或者您需要某种排序索引和数组中对象的地址,这可能写起来很麻烦但可行。所以问题是,你打算用这些对象做什么?从您使用的属性来看似乎很明显,看起来像是某种健身监测器,但是您打算如何处理数据并不明显,因此即使此模式有效也很难说出您可以做什么,请记住,它确实是数组的有效结构。
虽然如果您确实有两种类型的对象,有人可能会问为什么不简单地为每种类型创建一个数组,该数组将具有特定于其内容的支持数据拉取功能。但它会再次询问您打算做什么。
希望这对您的思考过程有所帮助。
更新...
不知道你打算做什么,这里是我建议你使用的数据结构格式。
let someArray = [
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
},
{
Steps: {
steps: 3500
},
HeartRate: {
heartrates: 4000
}
}
]
你的例子似乎有一些冗余,他们明确指出某物是一个对象,某物是该对象的 属性。 JavaScript 已经知道所有这些,因此无需为您创建的函数添加更多复杂性来使用这些函数,然后必须深入所有这些层。我建议你创建一个对象,每个对象都有一个你声明的类型的 属性 对象,如果你想根据需要扩展或压缩该对象。这将大大降低您的结构的复杂性。因此您会看到您有一个对象数组,这些对象包含表示您打算收集的数据的属性。
但这假设您收集的数据来自多个来源。再次需要更多地了解您要完成的工作。
Does the second schema correctly identify that "arr is an array of objects that have an integer named type an integer, and either an object named Steps or an object named Heartrate"?
是的,有点。您没有说明哪些属性 必须 存在,哪些属性不能同时存在,我们可以使用“required”和“oneOf”关键字来做到这一点。
这是更新后的架构,还修复了您在“心率”部分中犯的错误(您说的是“步数”而不是“心率”):
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"required": [ "type" ],
"oneOf": [
{ "required": ["Steps"] },
{ "required": ["HeartRate"] }
},
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"required": ["steps"],
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"required": ["heartrates"],
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}