【发布时间】:2017-02-08 21:01:52
【问题描述】:
我正在尝试从 JSON 数据生成一些代码。我将一些 AWS 备份数据放入 JSON 文件中。我抓取第一条记录并从那一行生成一个 json 模式。
当我在 Powershell 中加载架构时,我无法枚举属性,因为它们被反序列化为主要属性,而不是“属性”中的数组。这看起来像一个 json 模式的东西,其中属性列表显示为对象列表而不是数组,但我不确定。
我猜 Powershell JSON 解析器在那里看不到数组,所以只需将它们设为属性。
我真的不想手动遍历模式 JSON 来获取属性列表及其类型。
schema.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "",
"type": "object",
"properties": {
"definition": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"calcDefFilters": {
"type": "object",
"properties": {
"l": {
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"required": [],
"properties": {
"m": {
"type": "object",
"properties": {
"field": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"value": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"operator": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
}
},
"required": [
"field",
"value",
"operator"
]
}
}
}
}
},
"required": [
"l"
]
},
"calculationId": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"calcDefParameters": {
"type": "object",
"properties": {
"l": {
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"required": [],
"properties": {
"m": {
"type": "object",
"properties": {
"name": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"value": {
"type": "object",
"properties": {
"n": {
"type": "string",
"minLength": 1
}
},
"required": [
"n"
]
},
"type": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
}
},
"required": [
"name",
"value",
"type"
]
}
}
}
}
},
"required": [
"l"
]
},
"runId": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"type": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"externalId": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
},
"dealVersionId": {
"type": "object",
"properties": {
"s": {
"type": "string",
"minLength": 1
}
},
"required": [
"s"
]
}
},
"required": [
"definition",
"calcDefFilters",
"calculationId",
"calcDefParameters",
"runId",
"type",
"externalId",
"dealVersionId"
]
}
这是我在 Powershell 中得到的:
[DBG]: PS C:\Scripts>> $prop
definition : @{type=object; properties=; required=System.Object[]}
calcDefFilters : @{type=object; properties=; required=System.Object[]}
calculationId : @{type=object; properties=; required=System.Object[]}
calcDefParameters : @{type=object; properties=; required=System.Object[]}
runId : @{type=object; properties=; required=System.Object[]}
type : @{type=object; properties=; required=System.Object[]}
externalId : @{type=object; properties=; required=System.Object[]}
dealVersionId : @{type=object; properties=; required=System.Object[]}
想法?
【问题讨论】:
-
嗯,
properties不是一个数组,它的 json 语法是properties: [ ... ]而不是properties: { ... } -
对。所以 JSON 模式不会在数组中做属性。那我倒霉了?
-
嵌套的 NoteProperty 值怎么样?
-
我不想这么说,但我无法在 PowerShell 中解决这个问题。我将逻辑移至 C#,我几乎完成了。这仍然很奇怪,但如果你告诉它正确的事情,JSON.net 解析器似乎会做得更好。
标签: json powershell schema