我正在寻找如何为对象内的对象集合编写JSON模式。
{
"name": "Sadiq",
"age": 68,
"email": [
{
"emailid": "sadiq@gmail.com"
},
{
"emailid": "sadiq@yahoo.com"
}
],
"phone": [
{
"phonenumber": "301-215-8006"
},
{
"phonenumber": "301-215-8007"
}
]
}
答案 0 :(得分:6)
以下是编写此架构的一种可能方法:
{
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"required": true
},
"age": {
"type": "integer",
"required": true
},
"email": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"emailid": {
"type": "string",
"required": true
}
}
}
},
"phone": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"phonenumber": {
"type": "string",
"required": true
}
}
}
}
}
}
可能的改进是:
emailid
字段email
和phone
提取为顶级类型,并在上述架构中引用它们。答案 1 :(得分:1)
您可以尝试csonschema,这样可以更轻松地编写jsonschema。
name: 'string'
age: 'integer'
email: ['email']
phone: ['string']
答案 2 :(得分:0)
在python中,有json库可以帮助你根据需要编码或重新格式化